* pgg-epg.el: New file.
[elisp/epg.git] / pgg-epg.el
1 (require 'epg)
2 (eval-when-compile (require 'pgg))
3
4 (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase)
5   "This function is for internal use only.
6
7 Encrypt the current region between START and END.
8
9 If optional argument SIGN is non-nil, do a combined sign and encrypt.
10
11 If optional PASSPHRASE is not specified, it will be obtained from the
12 passphrase cache or user."
13   (let ((context (epg-make-context))
14         cipher)
15     (epg-context-set-armor context t)
16     (epg-context-set-textmode context pgg-text-mode)
17     (setq cipher (epg-encrypt-string context (buffer-substring start end)
18                                      (if pgg-encrypt-for-me
19                                          (cons pgg-default-user-id recipients)
20                                        recipients)
21                                      sign t))
22     (save-excursion
23       (set-buffer (get-buffer-create pgg-output-buffer))
24       (insert cipher))
25     t))
26
27 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
28   "This function is for internal use only.
29
30 Encrypt the current region between START and END with symmetric cipher.
31
32 If optional PASSPHRASE is not specified, it will be obtained from the
33 passphrase cache or user."
34   (pgg-epg-encrypt-region start end nil))
35
36 (defun pgg-epg-decrypt-region (start end &optional passphrase)
37   "This function is for internal use only.
38
39 Decrypt the current region between START and END.
40
41 If optional PASSPHRASE is not specified, it will be obtained from the
42 passphrase cache or user."
43   (let ((context (epg-make-context))
44         plain)
45     (epg-context-set-armor context t)
46     (epg-context-set-textmode context pgg-text-mode)
47     (setq plain (epg-decrypt-string context (buffer-substring start end)))
48     (save-excursion
49       (set-buffer (get-buffer-create pgg-output-buffer))
50       (insert plain))
51     t))
52
53 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
54   "This function is for internal use only.
55
56 Make detached signature from text between START and END.
57
58 If optional PASSPHRASE is not specified, it will be obtained from the
59 passphrase cache or user."
60   (let ((context (epg-make-context))
61         signature)
62     (epg-context-set-armor context t)
63     (epg-context-set-textmode context pgg-text-mode)
64     (setq signature (epg-sign-string context (buffer-substring start end)
65                                      (if cleartext
66                                          'cleartext
67                                        'detached)))
68     (save-excursion
69       (set-buffer (get-buffer-create pgg-output-buffer))
70       (insert signature))
71     t)
72
73 (defun pgg-epg-verify-region (start end &optional signature)
74   "This function is for internal use only.
75
76 Verify region between START and END as the detached signature SIGNATURE."
77   (let ((context (epg-make-context))
78         pointer)
79     (epg-context-set-armor context t)
80     (epg-context-set-textmode context pgg-text-mode)
81     (if signature
82         (epg-verify-file context signature (buffer-substring start end))
83       (epg-verify-string context (buffer-substring start end)))
84     (reverse (epg-context-result-for context 'verify)))
85
86 (defun pgg-epg-insert-key ()
87   "This function is for internal use only.
88
89 Insert public key at point."
90   (let ((context (epg-make-context))
91         pointer)
92     (epg-context-set-armor context t)
93     (epg-context-set-textmode context pgg-text-mode)
94     (insert (epg-export-keys context pgg-default-user-id))))
95
96 (defun pgg-epg-snarf-keys-region (start end)
97   "This function is for internal use only.
98
99 Add all public keys in region between START and END to the keyring."
100   (let ((context (epg-make-context))
101         pointer)
102     (epg-context-set-armor context t)
103     (epg-context-set-textmode context pgg-text-mode)
104     (epg-import-keys context (buffer-substring start end))))
105
106 (provide 'pgg-epg)
107
108 ;;; pgg-epg.el ends here