1 ;;; epa.el --- the EasyPG Assistant
2 ;; Copyright (C) 2006 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of EasyPG.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'wid-edit))
32 "The EasyPG Assistant"
35 (defcustom epa-armor nil
36 "If non-nil, epa commands create ASCII armored output."
40 (defcustom epa-textmode nil
41 "If non-nil, epa commands treat input files as text."
45 (defcustom epa-popup-info-window t
46 "If non-nil, status information from epa commands is displayed on
51 (defcustom epa-info-window-height 5
52 "Number of lines used to display status information."
56 (defgroup epa-faces nil
60 (defface epa-validity-high-face
61 '((((class color) (background dark))
62 (:foreground "PaleTurquoise" :bold t))
65 "Face used for displaying the high validity."
67 (defvar epa-validity-high-face 'epa-validity-high-face)
69 (defface epa-validity-medium-face
70 '((((class color) (background dark))
71 (:foreground "PaleTurquoise" :italic t))
74 "Face used for displaying the medium validity."
76 (defvar epa-validity-medium-face 'epa-validity-medium-face)
78 (defface epa-validity-low-face
81 "Face used for displaying the low validity."
83 (defvar epa-validity-low-face 'epa-validity-low-face)
85 (defface epa-validity-disabled-face
87 (:italic t :inverse-video t)))
88 "Face used for displaying the disabled validity."
90 (defvar epa-validity-disabled-face 'epa-validity-disabled-face)
92 (defface epa-string-face
95 (:foreground "lightyellow"))
98 (:foreground "blue4"))
101 "Face used for displaying the string."
103 (defvar epa-string-face 'epa-string-face)
105 (defface epa-mark-face
106 '((((class color) (background dark))
107 (:foreground "orange" :bold t))
109 (:foreground "red" :bold t)))
110 "Face used for displaying the high validity."
112 (defvar epa-mark-face 'epa-mark-face)
114 (defface epa-field-name-face
115 '((((class color) (background dark))
116 (:foreground "PaleTurquoise" :bold t))
118 "Face for the name of the attribute field."
120 (defvar epa-field-name-face 'epa-field-name-face)
122 (defface epa-field-body-face
123 '((((class color) (background dark))
124 (:foreground "turquoise" :italic t))
126 "Face for the body of the attribute field."
128 (defvar epa-field-body-face 'epa-field-body-face)
130 (defcustom epa-validity-face-alist
131 '((unknown . epa-validity-disabled-face)
132 (invalid . epa-validity-disabled-face)
133 (disabled . epa-validity-disabled-face)
134 (revoked . epa-validity-disabled-face)
135 (expired . epa-validity-disabled-face)
136 (none . epa-validity-low-face)
137 (undefined . epa-validity-low-face)
138 (never . epa-validity-low-face)
139 (marginal . epa-validity-medium-face)
140 (full . epa-validity-high-face)
141 (ultimate . epa-validity-high-face))
142 "An alist mapping validity values to faces."
146 (defcustom epa-font-lock-keywords
149 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
150 (1 epa-field-name-face)
151 (2 epa-field-body-face)))
152 "Default expressions to addon in epa-mode."
153 :type '(repeat (list string))
156 (defconst epa-pubkey-algorithm-letter-alist
164 (defvar epa-keys-buffer nil)
165 (defvar epa-key-buffer-alist nil)
167 (defvar epa-list-keys-arguments nil)
168 (defvar epa-info-buffer nil)
170 (defvar epa-keys-mode-map
171 (let ((keymap (make-sparse-keymap)))
172 (define-key keymap "m" 'epa-mark)
173 (define-key keymap "u" 'epa-unmark)
174 (define-key keymap "d" 'epa-decrypt-file)
175 (define-key keymap "v" 'epa-verify-file)
176 (define-key keymap "s" 'epa-sign-file)
177 (define-key keymap "e" 'epa-encrypt-file)
178 (define-key keymap "r" 'epa-delete-keys)
179 (define-key keymap "i" 'epa-import-keys)
180 (define-key keymap "o" 'epa-export-keys)
181 (define-key keymap "g" 'epa-list-keys)
182 (define-key keymap "n" 'next-line)
183 (define-key keymap "p" 'previous-line)
184 (define-key keymap " " 'scroll-up)
185 (define-key keymap [delete] 'scroll-down)
186 (define-key keymap "q" 'epa-exit-buffer)
189 (defvar epa-key-mode-map
190 (let ((keymap (make-sparse-keymap)))
191 (define-key keymap "q" 'bury-buffer)
194 (defvar epa-info-mode-map
195 (let ((keymap (make-sparse-keymap)))
196 (define-key keymap "q" 'delete-window)
199 (defvar epa-exit-buffer-function #'bury-buffer)
201 (define-widget 'epa-key 'push-button
202 "Button for representing a epg-key object."
204 :button-face-get 'epa--key-widget-button-face-get
205 :value-create 'epa--key-widget-value-create
206 :action 'epa--key-widget-action
207 :help-echo 'epa--key-widget-help-echo)
209 (defun epa--key-widget-action (widget &optional event)
210 (epa--show-key (widget-get widget :value)))
212 (defun epa--key-widget-value-create (widget)
213 (let* ((key (widget-get widget :value))
214 (primary-sub-key (car (epg-key-sub-key-list key)))
215 (primary-user-id (car (epg-key-user-id-list key))))
216 (insert (format "%c "
217 (if (epg-sub-key-validity primary-sub-key)
218 (car (rassq (epg-sub-key-validity primary-sub-key)
219 epg-key-validity-alist))
221 (epg-sub-key-id primary-sub-key)
224 (if (stringp (epg-user-id-string primary-user-id))
225 (epg-user-id-string primary-user-id)
226 (epg-decode-dn (epg-user-id-string primary-user-id)))
229 (defun epa--key-widget-button-face-get (widget)
230 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
231 (widget-get widget :value))))))
233 (cdr (assq validity epa-validity-face-alist))
236 (defun epa--key-widget-help-echo (widget)
238 (epg-sub-key-id (car (epg-key-sub-key-list
239 (widget-get widget :value))))))
241 (defun epa-keys-mode ()
242 "Major mode for `epa-list-keys'."
243 (kill-all-local-variables)
244 (buffer-disable-undo)
245 (setq major-mode 'epa-keys-mode
249 (use-local-map epa-keys-mode-map)
250 (make-local-variable 'font-lock-defaults)
251 (setq font-lock-defaults '(epa-font-lock-keywords t))
252 ;; In XEmacs, auto-initialization of font-lock is not effective
253 ;; if buffer-file-name is not set.
254 (font-lock-set-defaults)
255 (make-local-variable 'epa-exit-buffer-function)
256 (run-hooks 'epa-keys-mode-hook))
258 (defun epa-key-mode ()
259 "Major mode for a key description."
260 (kill-all-local-variables)
261 (buffer-disable-undo)
262 (setq major-mode 'epa-key-mode
266 (use-local-map epa-key-mode-map)
267 (make-local-variable 'font-lock-defaults)
268 (setq font-lock-defaults '(epa-font-lock-keywords t))
269 ;; In XEmacs, auto-initialization of font-lock is not effective
270 ;; if buffer-file-name is not set.
271 (font-lock-set-defaults)
272 (make-local-variable 'epa-exit-buffer-function)
273 (run-hooks 'epa-key-mode-hook))
275 (defun epa-info-mode ()
276 "Major mode for `epa-info-buffer'."
277 (kill-all-local-variables)
278 (buffer-disable-undo)
279 (setq major-mode 'epa-info-mode
283 (use-local-map epa-info-mode-map)
284 (run-hooks 'epa-info-mode-hook))
286 (defun epa-mark (&optional arg)
287 "Mark the current line.
288 If ARG is non-nil, unmark the current line."
290 (let ((inhibit-read-only t)
294 (setq properties (text-properties-at (point)))
296 (insert (if arg " " "*"))
297 (set-text-properties (1- (point)) (point) properties)
300 (defun epa-unmark (&optional arg)
301 "Unmark the current line.
302 If ARG is non-nil, mark the current line."
304 (epa-mark (not arg)))
306 (defun epa-toggle-mark ()
307 "Toggle the mark the current line."
309 (epa-mark (eq (char-after (save-excursion (beginning-of-line) (point))) ?*)))
311 (defun epa-exit-buffer ()
312 "Exit the current buffer.
313 `epa-exit-buffer-function' is called if it is set."
315 (funcall epa-exit-buffer-function))
318 (defun epa-list-keys (&optional name mode protocol)
320 (if current-prefix-arg
321 (let ((name (read-string "Pattern: "
322 (if epa-list-keys-arguments
323 (car epa-list-keys-arguments)))))
324 (list (if (equal name "") nil name)
325 (y-or-n-p "Secret keys? ")
326 (intern (completing-read "Protocol? "
327 '(("OpenPGP") ("CMS"))
329 (or epa-list-keys-arguments (list nil nil nil))))
330 (unless (and epa-keys-buffer
331 (buffer-live-p epa-keys-buffer))
332 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
333 (set-buffer epa-keys-buffer)
334 (let ((inhibit-read-only t)
337 (context (epg-make-context protocol)))
338 (unless (get-text-property point 'epa-list-keys)
339 (setq point (next-single-property-change point 'epa-list-keys)))
342 (or (next-single-property-change point 'epa-list-keys)
345 (epa-insert-keys context name mode)
348 (set-keymap-parent (current-local-map) widget-keymap))
349 (make-local-variable 'epa-list-keys-arguments)
350 (setq epa-list-keys-arguments (list name mode protocol))
351 (goto-char (point-min))
352 (pop-to-buffer (current-buffer)))
354 (defun epa-insert-keys (context name mode)
357 (narrow-to-region (point) (point))
358 (let ((keys (epg-list-keys context name mode))
363 (add-text-properties point (point)
364 (list 'epa-key (car keys)
369 (widget-create 'epa-key :value (car keys))
371 (setq keys (cdr keys))))
372 (add-text-properties (point-min) (point-max)
373 (list 'epa-list-keys t
379 (defun epa--marked-keys ()
381 (set-buffer epa-keys-buffer)
382 (goto-char (point-min))
384 (while (re-search-forward "^\\*" nil t)
385 (if (setq key (get-text-property (match-beginning 0)
387 (setq keys (cons key keys))))
391 (let ((key (get-text-property (point) 'epa-key)))
396 (defun epa-select-keys (context prompt &optional names secret)
397 "Display a user's keyring and ask him to select keys.
398 CONTEXT is an epg-context.
399 PROMPT is a string to prompt with.
400 NAMES is a list of strings to be matched with keys. If it is nil, all
402 If SECRET is non-nil, list secret keys instead of public keys."
404 (unless (and epa-keys-buffer
405 (buffer-live-p epa-keys-buffer))
406 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
407 (let ((inhibit-read-only t)
409 (set-buffer epa-keys-buffer)
413 :notify (lambda (&rest ignore) (abort-recursive-edit))
415 (substitute-command-keys
416 "Click here or \\[abort-recursive-edit] to cancel")
419 :notify (lambda (&rest ignore) (exit-recursive-edit))
421 (substitute-command-keys
422 "Click here or \\[exit-recursive-edit] to finish")
427 (epa-insert-keys context (car names) secret)
428 (if (get-text-property (point) 'epa-list-keys)
430 (goto-char (point-max))
431 (setq names (cdr names)))
434 (epa-insert-keys context nil secret)
435 (if (get-text-property (point) 'epa-list-keys)
437 (epa-insert-keys context nil nil)))
440 (set-keymap-parent (current-local-map) widget-keymap)
441 (setq epa-exit-buffer-function #'abort-recursive-edit)
442 (goto-char (point-min))
443 (pop-to-buffer (current-buffer)))
448 (if (get-buffer-window epa-keys-buffer)
449 (delete-window (get-buffer-window epa-keys-buffer)))
450 (kill-buffer epa-keys-buffer))))
452 (defun epa--show-key (key)
453 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
454 (entry (assoc (epg-sub-key-id primary-sub-key)
455 epa-key-buffer-alist))
456 (inhibit-read-only t)
460 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
461 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
462 (unless (and (cdr entry)
463 (buffer-live-p (cdr entry)))
464 (setcdr entry (generate-new-buffer
465 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
466 (set-buffer (cdr entry))
467 (make-local-variable 'epa-key)
470 (setq pointer (epg-key-user-id-list key))
474 (if (epg-user-id-validity (car pointer))
476 (car (rassq (epg-user-id-validity (car pointer))
477 epg-key-validity-alist)))
480 (if (stringp (epg-user-id-string (car pointer)))
481 (epg-user-id-string (car pointer))
482 (epg-decode-dn (epg-user-id-string (car pointer))))
484 (setq pointer (cdr pointer)))
485 (setq pointer (epg-key-sub-key-list key))
488 (if (epg-sub-key-validity (car pointer))
490 (car (rassq (epg-sub-key-validity (car pointer))
491 epg-key-validity-alist)))
494 (epg-sub-key-id (car pointer))
497 (epg-sub-key-length (car pointer)))
499 (cdr (assq (epg-sub-key-algorithm (car pointer))
500 epg-pubkey-algorithm-alist))
502 (format-time-string "%Y-%m-%d"
503 (epg-sub-key-creation-time (car pointer)))
504 (if (epg-sub-key-expiration-time (car pointer))
505 (format "\n\tExpires: %s"
506 (format-time-string "%Y-%m-%d"
507 (epg-sub-key-expiration-time
511 (mapconcat #'symbol-name
512 (epg-sub-key-capability (car pointer))
515 (epg-sub-key-fingerprint (car pointer))
517 (setq pointer (cdr pointer)))
518 (goto-char (point-min))
519 (pop-to-buffer (current-buffer))
522 (defun epa-display-info (info)
523 (if epa-popup-info-window
524 (save-selected-window
525 (unless epa-info-buffer
526 (setq epa-info-buffer (generate-new-buffer "*Info*")))
528 (set-buffer epa-info-buffer)
529 (let ((inhibit-read-only t)
534 (goto-char (point-min)))
535 (if (> (window-height)
536 epa-info-window-height)
537 (set-window-buffer (split-window nil (- (window-height)
538 epa-info-window-height))
540 (pop-to-buffer epa-info-buffer)
541 (if (> (window-height) epa-info-window-height)
542 (shrink-window (- (window-height) epa-info-window-height)))))
543 (message "%s" info)))
545 (defun epa-display-verify-result (verify-result)
546 (epa-display-info (epg-verify-result-to-string verify-result)))
547 (make-obsolete 'epa-display-verify-result 'epa-display-info)
549 (defun epa-passphrase-callback-function (context key-id handback)
551 (read-passwd "Passphrase for symmetric encryption: "
552 (eq (epg-context-operation context) 'encrypt))
555 "Passphrase for PIN: "
556 (let ((entry (assoc key-id epg-user-id-alist)))
558 (format "Passphrase for %s %s: " key-id (cdr entry))
559 (format "Passphrase for %s: " key-id)))))))
561 (defun epa-progress-callback-function (context what char current total
563 (message "%s: %d%% (%d/%d)" what
564 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
568 (defun epa-decrypt-file (file)
570 (interactive "fFile: ")
571 (setq file (expand-file-name file))
572 (let* ((default-name (file-name-sans-extension file))
573 (plain (expand-file-name
575 (concat "To file (default "
576 (file-name-nondirectory default-name)
578 (file-name-directory default-name)
580 (context (epg-make-context)))
581 (epg-context-set-passphrase-callback context
582 #'epa-passphrase-callback-function)
583 (epg-context-set-progress-callback context
584 #'epa-progress-callback-function)
585 (message "Decrypting %s..." (file-name-nondirectory file))
586 (epg-decrypt-file context file plain)
587 (message "Decrypting %s...done" (file-name-nondirectory file))
588 (if (epg-context-result-for context 'verify)
589 (epa-display-info (epg-verify-result-to-string
590 (epg-context-result-for context 'verify))))))
593 (defun epa-verify-file (file)
595 (interactive "fFile: ")
596 (setq file (expand-file-name file))
597 (let* ((context (epg-make-context))
598 (plain (if (equal (file-name-extension file) "sig")
599 (file-name-sans-extension file))))
600 (epg-context-set-progress-callback context
601 #'epa-progress-callback-function)
602 (message "Verifying %s..." (file-name-nondirectory file))
603 (epg-verify-file context file plain)
604 (message "Verifying %s...done" (file-name-nondirectory file))
605 (if (epg-context-result-for context 'verify)
606 (epa-display-info (epg-verify-result-to-string
607 (epg-context-result-for context 'verify))))))
610 (defun epa-sign-file (file signers mode)
611 "Sign FILE by SIGNERS keys selected."
613 (list (expand-file-name (read-file-name "File: "))
614 (epa-select-keys (epg-make-context) "Select keys for signing.
615 If no one is selected, default secret key is used. "
619 (message "Signature type (n,c,d,?) ")
620 (let ((c (read-char)))
622 (throw 'done 'clear))
624 (throw 'done 'detached))
626 (with-output-to-temp-buffer "*Help*"
628 (set-buffer standard-output)
630 n - Create a normal signature
631 c - Create a cleartext signature
632 d - Create a detached signature
636 (throw 'done nil))))))))
637 (let ((signature (concat file
639 (not (memq mode '(nil t normal detached))))
641 (if (memq mode '(t detached))
644 (context (epg-make-context)))
645 (epg-context-set-armor context epa-armor)
646 (epg-context-set-textmode context epa-textmode)
647 (epg-context-set-signers context signers)
648 (epg-context-set-passphrase-callback context
649 #'epa-passphrase-callback-function)
650 (epg-context-set-progress-callback context
651 #'epa-progress-callback-function)
652 (message "Signing %s..." (file-name-nondirectory file))
653 (epg-sign-file context file signature mode)
654 (message "Signing %s...done" (file-name-nondirectory file))))
657 (defun epa-encrypt-file (file recipients)
658 "Encrypt FILE for RECIPIENTS."
660 (list (expand-file-name (read-file-name "File: "))
661 (epa-select-keys (epg-make-context) "Select recipients for encryption.
662 If no one is selected, symmetric encryption will be performed. ")))
663 (let ((cipher (concat file (if epa-armor ".asc" ".gpg")))
664 (context (epg-make-context)))
665 (epg-context-set-armor context epa-armor)
666 (epg-context-set-textmode context epa-textmode)
667 (epg-context-set-passphrase-callback context
668 #'epa-passphrase-callback-function)
669 (epg-context-set-progress-callback context
670 #'epa-progress-callback-function)
671 (message "Encrypting %s..." (file-name-nondirectory file))
672 (epg-encrypt-file context file recipients cipher)
673 (message "Encrypting %s...done" (file-name-nondirectory file))))
676 (defun epa-decrypt-region (start end)
677 "Decrypt the current region between START and END.
679 Don't use this command in Lisp programs!"
682 (let ((context (epg-make-context))
684 (epg-context-set-passphrase-callback context
685 #'epa-passphrase-callback-function)
686 (epg-context-set-progress-callback context
687 #'epa-progress-callback-function)
688 (message "Decrypting...")
689 (setq plain (epg-decrypt-string context (buffer-substring start end)))
690 (message "Decrypting...done")
691 (delete-region start end)
693 (insert (decode-coding-string plain coding-system-for-read))
694 (if (epg-context-result-for context 'verify)
695 (epa-display-info (epg-verify-result-to-string
696 (epg-context-result-for context 'verify)))))))
699 (defun epa-decrypt-armor-in-region (start end)
700 "Decrypt OpenPGP armors in the current region between START and END.
702 Don't use this command in Lisp programs!"
706 (narrow-to-region start end)
708 (let (armor-start armor-end charset coding-system)
709 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
710 (setq armor-start (match-beginning 0)
711 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
714 (error "No armor tail"))
715 (goto-char armor-start)
716 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
717 (setq charset (match-string 1)))
718 (if coding-system-for-read
719 (setq coding-system coding-system-for-read)
721 (setq coding-system (intern (downcase charset)))
722 (setq coding-system 'utf-8)))
723 (let ((coding-system-for-read coding-system))
724 (epa-decrypt-region start end)))))))
727 (defun epa-verify-region (start end)
728 "Verify the current region between START and END.
730 Don't use this command in Lisp programs!"
732 (let ((context (epg-make-context)))
733 (epg-context-set-progress-callback context
734 #'epa-progress-callback-function)
735 (epg-verify-string context
736 (encode-coding-string
737 (buffer-substring start end)
738 coding-system-for-write))
739 (if (epg-context-result-for context 'verify)
740 (epa-display-info (epg-verify-result-to-string
741 (epg-context-result-for context 'verify))))))
744 (defun epa-verify-cleartext-in-region (start end)
745 "Verify OpenPGP cleartext signed messages in the current region
746 between START and END.
748 Don't use this command in Lisp programs!"
752 (narrow-to-region start end)
754 (let (armor-start armor-end)
755 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
757 (setq armor-start (match-beginning 0))
758 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
760 (error "Invalid cleartext signed message"))
761 (setq armor-end (re-search-forward
762 "^-----END PGP SIGNATURE-----$"
765 (error "No armor tail"))
766 (epa-verify-region armor-start armor-end))))))
769 (defun epa-sign-region (start end signers mode)
770 "Sign the current region between START and END by SIGNERS keys selected.
772 Don't use this command in Lisp programs!"
774 (list (region-beginning) (region-end)
775 (epa-select-keys (epg-make-context) "Select keys for signing.
776 If no one is selected, default secret key is used. "
780 (message "Signature type (n,c,d,?) ")
781 (let ((c (read-char)))
783 (throw 'done 'clear))
785 (throw 'done 'detached))
787 (with-output-to-temp-buffer "*Help*"
789 (set-buffer standard-output)
791 n - Create a normal signature
792 c - Create a cleartext signature
793 d - Create a detached signature
797 (throw 'done nil))))))))
799 (let ((context (epg-make-context))
801 (epg-context-set-armor context epa-armor)
802 (epg-context-set-textmode context epa-textmode)
803 (epg-context-set-signers context signers)
804 (epg-context-set-passphrase-callback context
805 #'epa-passphrase-callback-function)
806 (epg-context-set-progress-callback context
807 #'epa-progress-callback-function)
808 (message "Signing...")
809 (setq signature (epg-sign-string context
810 (encode-coding-string
811 (buffer-substring start end)
812 coding-system-for-write)
814 (message "Signing...done")
815 (delete-region start end)
816 (insert (decode-coding-string signature coding-system-for-read)))))
819 (defun epa-encrypt-region (start end recipients)
820 "Encrypt the current region between START and END for RECIPIENTS.
822 Don't use this command in Lisp programs!"
824 (list (region-beginning) (region-end)
825 (epa-select-keys (epg-make-context) "Select recipients for encryption.
826 If no one is selected, symmetric encryption will be performed. ")))
828 (let ((context (epg-make-context))
830 (epg-context-set-armor context epa-armor)
831 (epg-context-set-textmode context epa-textmode)
832 (epg-context-set-passphrase-callback context
833 #'epa-passphrase-callback-function)
834 (epg-context-set-progress-callback context
835 #'epa-progress-callback-function)
836 (message "Encrypting...")
837 (setq cipher (epg-encrypt-string context
838 (encode-coding-string
839 (buffer-substring start end)
840 coding-system-for-write)
842 (message "Encrypting...done")
843 (delete-region start end)
847 (defun epa-delete-keys (keys &optional allow-secret)
848 "Delete selected KEYS.
850 Don't use this command in Lisp programs!"
852 (let ((keys (epa--marked-keys)))
854 (error "No keys selected"))
856 (eq (nth 1 epa-list-keys-arguments) t))))
857 (let ((context (epg-make-context)))
858 (message "Deleting...")
859 (epg-delete-keys context keys allow-secret)
860 (message "Deleting...done")
861 (apply #'epa-list-keys epa-list-keys-arguments)))
864 (defun epa-import-keys (file)
865 "Import keys from FILE.
867 Don't use this command in Lisp programs!"
868 (interactive "fFile: ")
869 (setq file (expand-file-name file))
870 (let ((context (epg-make-context)))
871 (message "Importing %s..." (file-name-nondirectory file))
874 (epg-import-keys-from-file context file)
875 (message "Importing %s...done" (file-name-nondirectory file)))
877 (message "Importing %s...failed" (file-name-nondirectory file))))
878 (if (epg-context-result-for context 'import)
879 (epa-display-info (epg-import-result-to-string
880 (epg-context-result-for context 'import))))
881 (if (eq major-mode 'epa-keys-mode)
882 (apply #'epa-list-keys epa-list-keys-arguments))))
885 (defun epa-export-keys (keys file)
886 "Export selected KEYS to FILE.
888 Don't use this command in Lisp programs!"
890 (let ((keys (epa--marked-keys))
893 (error "No keys selected"))
896 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
897 (if epa-armor ".asc" ".gpg"))
902 (concat "To file (default "
903 (file-name-nondirectory default-name)
905 (file-name-directory default-name)
907 (let ((context (epg-make-context)))
908 (epg-context-set-armor context epa-armor)
909 (message "Exporting to %s..." (file-name-nondirectory file))
910 (epg-export-keys-to-file context keys file)
911 (message "Exporting to %s...done" (file-name-nondirectory file))))
914 (defun epa-sign-keys (keys &optional local)
916 If a prefix-arg is specified, the signature is marked as non exportable.
918 Don't use this command in Lisp programs!"
920 (let ((keys (epa--marked-keys)))
922 (error "No keys selected"))
923 (list keys current-prefix-arg)))
924 (let ((context (epg-make-context)))
925 (epg-context-set-passphrase-callback context
926 #'epa-passphrase-callback-function)
927 (epg-context-set-progress-callback context
928 #'epa-progress-callback-function)
929 (message "Signing keys...")
930 (epg-sign-keys context keys local)
931 (message "Signing keys...done")))
932 (make-obsolete 'epa-sign-keys "Do not use.")