org-crypt.el 6.2 KB

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