/[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 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC
# Line 1  Line 1 
1  /* wptSymEnc.cpp - Symmetric encryption support  /* wptSymEnc.cpp - Symmetric encryption support
2   *      Copyright (C) 2002, 2003, 2004 Timo Schulz   *      Copyright (C) 2002, 2003, 2004, 2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 29  Line 29 
29  #include "wptTypes.h"  #include "wptTypes.h"
30  #include "wptErrors.h"  #include "wptErrors.h"
31    
32    
33    /* Simple passphrase callback. The hook pointer is the passphrase itself.
34       Return value: 0 on success. */
35    gpgme_error_t
36    sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
37                       int prev_was_bad, int fd)
38    {
39        const char *pass = (const char*)hook;
40        HANDLE hd = (HANDLE)fd;
41        DWORD n;
42    
43        if (!pass)
44            return gpg_error (GPG_ERR_INV_ARG);
45    
46        /* XXX: check for passphrase.enter. */
47        if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
48            log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
49        if (!WriteFile (hd, "\n", 1, &n, NULL))
50            log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
51        
52        return 0;
53    }
54    
55    
56    /* Perform symmetric encryption on the current clipboard data.
57       Return 0 for success. */
58  int  int
59  gpgme_encrypt_symmetric (void)  gpg_encrypt_symmetric (void)
60  {  {
61      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
62        gpgme_data_t plain=NULL;
63        gpgme_data_t ciph=NULL;
64      gpgme_error_t rc;      gpgme_error_t rc;
65      char * pass = NULL;      char *pass = NULL;
66      int cancel = 0;      int cancel = 0;
67            
68      pass = request_passphrase2 (_("Symmetric Encryption"), &cancel);      pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel);
69      if (cancel)      if (cancel)
70          return WPTERR_GENERAL;          return 0;
71      rc = gpgme_new (&ctx);      rc = gpgme_new (&ctx);
72      if (rc)      if (rc)
73          BUG (0);          goto leave;
74      gpgme_set_passphrase (ctx, pass);      rc = gpg_data_new_from_clipboard (&plain, 0);
75      rc = gpgme_op_clip_symenc (ctx);      if (rc)
76      sfree_if_alloc (pass);          goto leave;
77        rc = gpgme_data_new (&ciph);
78        gpgme_set_armor (ctx, 1);
79    
80        gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
81    
82        rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph);    
83      if (rc)      if (rc)
84          msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);          msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);
85      else      else {
86          show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));          show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
87            gpg_data_release_and_set_clipboard (ciph, 1);
88            ciph = NULL;
89        }
90    
91    leave:
92        if (plain)
93            gpgme_data_release (plain);
94        if (ciph)
95            gpgme_data_release (ciph);
96      gpgme_release (ctx);      gpgme_release (ctx);
97        sfree_if_alloc (pass);
98      return rc? WPTERR_GENERAL : 0;      return rc? WPTERR_GENERAL : 0;
 } /* gpgme_encrypt_symmetric */  
99    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26