Compile org-crypt.el without (require 'epg)
[org-mode.git] / lisp / org-crypt.el
blob24aeba5925cd9f6bc874e5ba9c5f569df115b4fb
1 ;;; org-crypt.el --- Public key encryption for org-mode entries
3 ;; Copyright (C) 2009 Peter Jones <pjones@pmade.com>
4 ;; Copyright (C) 2007 John Wiegley <johnw@gnu.org>
6 ;; Emacs Lisp Archive Entry
7 ;; Filename: org-crypt.el
8 ;; Version: 0.4
9 ;; Keywords: org-mode
10 ;; Author: John Wiegley <johnw@gnu.org>
11 ;; Maintainer: Peter Jones <pjones@pmade.com>
12 ;; Description: Adds public key encryption to org-mode buffers
13 ;; URL: http://www.newartisans.com/software/emacs.html
14 ;; Compatibility: Emacs22
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; Right now this is just a set of functions to play with. It depends
34 ;; on the epg library. Here's how you would use it:
36 ;; 1. To mark an entry for encryption, tag the heading with "crypt".
37 ;; You can change the tag to any complex tag matching string by
38 ;; setting the `org-crypt-tag-matcher' variable.
40 ;; 2. Set the encryption key to use in the `org-crypt-key' variable,
41 ;; or use `M-x org-set-property' to set the property CRYPTKEY to
42 ;; any address in your public keyring. The text of the entry (but
43 ;; not its properties or headline) will be encrypted for this user.
44 ;; For them to read it, the corresponding secret key must be
45 ;; located in the secret key ring of the account where you try to
46 ;; decrypt it. This makes it possible to leave secure notes that
47 ;; only the intended recipient can read in a shared-org-mode-files
48 ;; scenario.
50 ;; 3. To later decrypt an entry, use `org-decrypt-entries' or
51 ;; `org-decrypt-entry'. It might be useful to bind this to a key,
52 ;; like C-c C-/. I hope that in the future, C-c C-r can be might
53 ;; overloaded to also decrypt an entry if it's encrypted, since
54 ;; that fits nicely with the meaning of "reveal".
56 ;; 4. To automatically encrypt all necessary entries when saving a
57 ;; file, call `org-crypt-use-before-save-magic' after loading
58 ;; org-crypt.el.
60 ;; TODO:
61 ;; - Allow symmetric encryption as well
63 ;;; Thanks:
65 ;; - Carsten Dominik
66 ;; - Vitaly Ostanin
68 (require 'org)
70 (declare-function epg-decrypt-string "epg" (context cipher))
71 (declare-function epg-list-keys "epg" (context &optional name mode))
72 (declare-function epg-make-context "epg"
73 (&optional protocol armor textmode include-certs
74 cipher-algorithm digest-algorithm
75 compress-algorithm))
76 (declare-function epg-encrypt-string "epg"
77 (context plain recipients &optional sign always-trust))
79 (defgroup org-crypt nil
80 "Org Crypt"
81 :tag "Org Crypt" :group 'org)
83 (defcustom org-crypt-tag-matcher "crypt"
84 "The tag matcher used to find headings whose contents should be
85 encrypted. See the \"Match syntax\" section of the org manual
86 for more details."
87 :type 'string :group 'org-crypt)
89 (defcustom org-crypt-key nil
90 "The default key to use when encrypting the contents of a
91 heading. This can also be overridden in the CRYPTKEY property."
92 :type 'string :group 'org-crypt)
94 (defun org-crypt-key-for-heading ()
95 "Returns the encryption key for the current heading."
96 (save-excursion
97 (org-back-to-heading t)
98 (or (org-entry-get nil "CRYPTKEY" 'selective)
99 org-crypt-key
100 (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
101 (error "No crypt key set"))))
103 (defun org-encrypt-entry ()
104 "Encrypt the content of the current headline."
105 (interactive)
106 (require 'epg)
107 (save-excursion
108 (org-back-to-heading t)
109 (forward-line)
110 (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
111 (let ((folded (org-invisible-p))
112 (epg-context (epg-make-context nil t t))
113 (crypt-key (org-crypt-key-for-heading))
114 (beg (point))
115 end encrypted-text)
116 (org-end-of-subtree t t)
117 (org-back-over-empty-lines)
118 (setq end (point)
119 encrypted-text
120 (epg-encrypt-string
121 epg-context
122 (buffer-substring-no-properties beg end)
123 (epg-list-keys epg-context crypt-key)))
124 (delete-region beg end)
125 (insert encrypted-text)
126 (when folded
127 (save-excursion
128 (org-back-to-heading t)
129 (hide-subtree)))
130 nil))))
132 (defun org-decrypt-entry ()
133 (interactive)
134 (require 'epg)
135 (save-excursion
136 (org-back-to-heading t)
137 (forward-line)
138 (when (looking-at "-----BEGIN PGP MESSAGE-----")
139 (let* ((beg (point))
140 (end (save-excursion
141 (search-forward "-----END PGP MESSAGE-----")
142 (forward-line)
143 (point)))
144 (epg-context (epg-make-context nil t t))
145 (decrypted-text
146 (decode-coding-string
147 (epg-decrypt-string
148 epg-context
149 (buffer-substring-no-properties beg end))
150 'utf-8)))
151 (delete-region beg end)
152 (insert decrypted-text)
153 nil))))
155 (defun org-encrypt-entries ()
156 (interactive)
157 (org-scan-tags
158 'org-encrypt-entry
159 (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
161 (defun org-decrypt-entries ()
162 (interactive)
163 (org-scan-tags
164 'org-decrypt-entry
165 (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
167 (defun org-crypt-use-before-save-magic ()
168 "Adds a hook that will automatically encrypt entries before a
169 file is saved to disk."
170 (add-hook
171 'org-mode-hook
172 (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
174 (provide 'org-crypt)
176 ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
178 ;;; org-crypt.el ends here