* epa-setup.el: New file.
[elisp/epg.git] / epa-dired.el
1 (require 'epa)
2 (require 'dired)
3
4 (defvar epa-dired-map
5   (let ((keymap (make-sparse-keymap)))
6     (define-key keymap "d" 'epa-dired-do-decrypt)
7     (define-key keymap "v" 'epa-dired-do-verify)
8     (define-key keymap "s" 'epa-dired-do-sign)
9     (define-key keymap "e" 'epa-dired-do-encrypt)
10     keymap))
11
12 (fset 'epa-dired-prefix epa-dired-map)
13
14 (defun epa-dired-setup ()
15   (add-hook 'dired-mode-hook 'epa-dired-mode-hook))
16
17 (defun epa-dired-mode-hook ()
18   (define-key dired-mode-map ":" 'epa-dired-prefix))
19
20 (defun epa-dired-decrypt-file (file)
21   (let ((context (epg-make-context)))
22     (message "Decrypting %s..." (file-name-nondirectory file))
23     (epg-decrypt-file context file)
24     (message "Decrypting %s...done" (file-name-nondirectory file))))
25
26 (defun epa-dired-do-decrypt ()
27   (interactive)
28   (let ((file-list (dired-get-marked-files)))
29     (while file-list
30       (epa-dired-decrypt-file (car file-list))
31       (setq file-list (cdr file-list)))))
32
33 (defun epa-dired-verify-file (file)
34   (let* ((context (epg-make-context))
35          (plain (if (equal (file-name-extension file) "sig")
36                     (file-name-sans-extension file))))
37     (message "Verifying %s..." (file-name-nondirectory file))
38     (epg-verify-file context file plain)
39     (message "Verifying %s...done" (file-name-nondirectory file))))
40
41 (defun epa-dired-do-verify ()
42   (interactive)
43   (let ((file-list (dired-get-marked-files)))
44     (while file-list
45       (epa-dired-verify-file (car file-list))
46       (setq file-list (cdr file-list)))))
47
48 (defun epa-dired-sign-file (file detached)
49   (let ((signature (concat file (if detached ".sig" ".gpg")))
50         (context (epg-make-context)))
51     (message "Signing %s..." (file-name-nondirectory file))
52     (epg-sign-file context
53                    file
54                    signature)
55     (message "Signing %s...done" (file-name-nondirectory file))))
56
57 (defun epa-dired-do-sign (&optional detached)
58   (interactive "p")
59   (let ((file-list (dired-get-marked-files)))
60     (while file-list
61       (epa-dired-sign-file (expand-file-name (car file-list)) detached)
62       (setq file-list (cdr file-list)))
63     (revert-buffer)))
64
65 (defun epa-dired-encrypt-file (keys file)
66   (let ((cipher (concat file ".gpg"))
67         (context (epg-make-context)))
68     (message "Encrypting %s..." (file-name-nondirectory file))
69     (epg-encrypt-file context
70                       file
71                       (mapcar (lambda (key)
72                                 (epg-sub-key-id
73                                  (car (epg-key-sub-key-list key))))
74                               keys)
75                       cipher)
76     (message "Encrypting %s...done" (file-name-nondirectory file))))
77
78 (defun epa-dired-do-encrypt ()
79   (interactive)
80   (let ((file-list (dired-get-marked-files)))
81     (while file-list
82       (epa-ask-keys "Select keys for encryption.  \
83 If no keys are selected, symmetric encryption will be performed.  "
84                     #'epa-dired-encrypt-file nil
85                     (expand-file-name (car file-list)))
86       (setq file-list (cdr file-list)))))
87
88 (provide 'epa-dired)
89
90 ;;; epa-dired.el ends here