/[winpt]/trunk/Src/wptSymEnc.cpp
ViewVC logotype

Diff of /trunk/Src/wptSymEnc.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 226 by twoaday, Mon Jun 12 13:40:21 2006 UTC
# Line 1  Line 1 
 /* wptSymEnc.cpp - Symmetric encryption support  
  *      Copyright (C) 2002, 2003, 2004 Timo Schulz  
  *  
  * This file is part of WinPT.  
  *  
  * WinPT is free software; you can redistribute it and/or  
  * modify it under the terms of the GNU General Public License  
  * as published by the Free Software Foundation; either version 2  
  * of the License, or (at your option) any later version.  
  *    
  * WinPT is distributed in the hope that it will be useful,  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
  * General Public License for more details.  
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
  */  
   
 #include <windows.h>  
   
 #include "wptGPG.h"  
 #include "wptCommonCtl.h"  
 #include "wptContext.h"  
 #include "wptDlgs.h"  
 #include "wptNLS.h"  
 #include "wptUTF8.h"  
 #include "wptTypes.h"  
 #include "wptErrors.h"  
   
 int  
 gpgme_encrypt_symmetric (void)  
 {  
     gpgme_ctx_t ctx;  
     gpgme_error_t rc;  
     char * pass = NULL;  
     int cancel = 0;  
       
     pass = request_passphrase2 (_("Symmetric Encryption"), &cancel);  
     if (cancel)  
         return WPTERR_GENERAL;  
     rc = gpgme_new (&ctx);  
     if (rc)  
         BUG (0);  
     gpgme_set_passphrase (ctx, pass);  
     rc = gpgme_op_clip_symenc (ctx);  
     sfree_if_alloc (pass);  
     if (rc)  
         msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);  
     else  
         show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));  
     gpgme_release (ctx);  
     return rc? WPTERR_GENERAL : 0;  
 } /* gpgme_encrypt_symmetric */  
1    /* wptSymEnc.cpp - Symmetric encryption support
2     *      Copyright (C) 2002-2006 Timo Schulz
3     *
4     * This file is part of WinPT.
5     *
6     * WinPT is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *  
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21    #ifdef HAVE_CONFIG_H
22    #include <config.h>
23    #endif
24    
25    #include <windows.h>
26    
27    #include "wptGPG.h"
28    #include "wptCommonCtl.h"
29    #include "wptContext.h"
30    #include "wptDlgs.h"
31    #include "wptNLS.h"
32    #include "wptUTF8.h"
33    #include "wptTypes.h"
34    #include "wptErrors.h"
35    
36    
37    /* Simple passphrase callback. The hook pointer is the passphrase itself.
38       Return value: 0 on success. */
39    gpgme_error_t
40    sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
41                       int prev_was_bad, int fd)
42    {
43        const char *pass = (const char*)hook;
44        HANDLE hd = (HANDLE)fd;
45        DWORD n;
46    
47        if (!pass)
48            return gpg_error (GPG_ERR_INV_ARG);
49    
50        /* XXX: check for passphrase.enter. */
51        if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
52            log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
53        if (!WriteFile (hd, "\n", 1, &n, NULL))
54            log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
55        
56        return 0;
57    }
58    
59    
60    /* Perform symmetric encryption on the current clipboard data.
61       Return 0 for success. */
62    gpgme_error_t
63    gpg_clip_sym_encrypt (void)
64    {
65        gpgme_ctx_t ctx;
66        gpgme_data_t plain=NULL;
67        gpgme_data_t ciph=NULL;
68        gpgme_error_t rc;
69        char *pass = NULL;
70        int cancel = 0;
71        
72        pass = request_passphrase2 (_("Symmetric Encryption"),
73                                    PASSDLG_NOTEMPTY, &cancel);
74        if (cancel)
75            return 0;
76        rc = gpgme_new (&ctx);
77        if (rc)
78            goto leave;
79        rc = gpg_data_new_from_clipboard (&plain, 0);
80        if (rc)
81            goto leave;
82        rc = gpgme_data_new (&ciph);
83        gpgme_set_armor (ctx, 1);
84        gpgme_set_textmode (ctx, 1);
85    
86        gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
87    
88        rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph);
89        if (rc)
90            msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);
91        else {
92            show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
93            gpg_data_release_and_set_clipboard (ciph, 1);
94            ciph = NULL;
95        }
96    
97    leave:
98        if (plain)
99            gpgme_data_release (plain);
100        if (ciph)
101            gpgme_data_release (ciph);
102        gpgme_release (ctx);
103        sfree_if_alloc (pass);
104        return rc;
105    }

Legend:
Removed from v.2  
changed lines
  Added in v.226

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26