OSDN Git Service

Fixed.
[epg/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       (erase-buffer)
25       (insert cipher))
26     t))
27
28 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
29   "This function is for internal use only.
30
31 Encrypt the current region between START and END with symmetric cipher.
32
33 If optional PASSPHRASE is not specified, it will be obtained from the
34 passphrase cache or user."
35   (pgg-epg-encrypt-region start end nil))
36
37 (defun pgg-epg-decrypt-region (start end &optional passphrase)
38   "This function is for internal use only.
39
40 Decrypt the current region between START and END.
41
42 If optional PASSPHRASE is not specified, it will be obtained from the
43 passphrase cache or user."
44   (let ((context (epg-make-context))
45         plain)
46     (epg-context-set-armor context t)
47     (epg-context-set-textmode context pgg-text-mode)
48     (setq plain (epg-decrypt-string context (buffer-substring start end)))
49     (save-excursion
50       (set-buffer (get-buffer-create pgg-output-buffer))
51       (erase-buffer)
52       (insert plain))
53     t))
54
55 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
56   "This function is for internal use only.
57
58 Make detached signature from text between START and END.
59
60 If optional PASSPHRASE is not specified, it will be obtained from the
61 passphrase cache or user."
62   (let ((context (epg-make-context))
63         signature)
64     (epg-context-set-armor context t)
65     (epg-context-set-textmode context pgg-text-mode)
66     (setq signature (epg-sign-string context (buffer-substring start end)
67                                      (if cleartext
68                                          'cleartext
69                                        'detached)))
70     (save-excursion
71       (set-buffer (get-buffer-create pgg-output-buffer))
72       (insert signature))
73     t))
74
75 (defun pgg-epg-verify-region (start end &optional signature)
76   "This function is for internal use only.
77
78 Verify region between START and END as the detached signature SIGNATURE."
79   (let ((context (epg-make-context))
80         pointer)
81     (epg-context-set-armor context t)
82     (epg-context-set-textmode context pgg-text-mode)
83     (if signature
84         (epg-verify-file context signature (buffer-substring start end))
85       (epg-verify-string context (buffer-substring start end)))
86     (setq signature (reverse (epg-context-result-for context 'verify))
87           pointer signature)
88     (save-excursion
89       (set-buffer (get-buffer-create pgg-errors-buffer))
90       (erase-buffer)
91       (while pointer
92         (insert (format "%s: %s %s %s\n"
93                         (epg-signature-status (car pointer))
94                         (epg-signature-key-id (car pointer))
95                         (epg-signature-user-id (car pointer))
96                         (epg-signature-validity (car pointer))))
97         (setq pointer (cdr pointer))))
98     signature))
99
100 (defun pgg-epg-insert-key ()
101   "This function is for internal use only.
102
103 Insert public key at point."
104   (let ((context (epg-make-context))
105         pointer)
106     (epg-context-set-armor context t)
107     (epg-context-set-textmode context pgg-text-mode)
108     (insert (epg-export-keys context pgg-default-user-id))))
109
110 (defun pgg-epg-snarf-keys-region (start end)
111   "This function is for internal use only.
112
113 Add all public keys in region between START and END to the keyring."
114   (let ((context (epg-make-context))
115         pointer)
116     (epg-context-set-armor context t)
117     (epg-context-set-textmode context pgg-text-mode)
118     (epg-import-keys context (buffer-substring start end))))
119
120 (provide 'pgg-epg)
121
122 ;;; pgg-epg.el ends here