org-crypt.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.36trans
  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. ;; If the key is not set, org-crypt will default to symmetric encryption.
  42. ;;
  43. ;; 3. To later decrypt an entry, use `org-decrypt-entries' or
  44. ;; `org-decrypt-entry'. It might be useful to bind this to a key,
  45. ;; like C-c C-/. I hope that in the future, C-c C-r can be might
  46. ;; overloaded to also decrypt an entry if it's encrypted, since
  47. ;; that fits nicely with the meaning of "reveal".
  48. ;;
  49. ;; 4. To automatically encrypt all necessary entries when saving a
  50. ;; file, call `org-crypt-use-before-save-magic' after loading
  51. ;; org-crypt.el.
  52. ;;
  53. ;; TODO:
  54. ;; - Allow symmetric encryption as well
  55. ;;; Thanks:
  56. ;; - Carsten Dominik
  57. ;; - Vitaly Ostanin
  58. (require 'org)
  59. (declare-function epg-decrypt-string "epg" (context cipher))
  60. (declare-function epg-list-keys "epg" (context &optional name mode))
  61. (declare-function epg-make-context "epg"
  62. (&optional protocol armor textmode include-certs
  63. cipher-algorithm digest-algorithm
  64. compress-algorithm))
  65. (declare-function epg-encrypt-string "epg"
  66. (context plain recipients &optional sign always-trust))
  67. (defgroup org-crypt nil
  68. "Org Crypt"
  69. :tag "Org Crypt" :group 'org)
  70. (defcustom org-crypt-tag-matcher "crypt"
  71. "The tag matcher used to find headings whose contents should be
  72. encrypted. See the \"Match syntax\" section of the org manual
  73. for more details."
  74. :type 'string :group 'org-crypt)
  75. (defcustom org-crypt-key nil
  76. "The default key to use when encrypting the contents of a
  77. heading. This can also be overridden in the CRYPTKEY property."
  78. :type 'string :group 'org-crypt)
  79. (defun org-crypt-key-for-heading ()
  80. "Returns the encryption key for the current heading."
  81. (save-excursion
  82. (org-back-to-heading t)
  83. (or (org-entry-get nil "CRYPTKEY" 'selective)
  84. org-crypt-key
  85. (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
  86. (message "No crypt key set, using symmetric encryption."))))
  87. (defun org-encrypt-entry ()
  88. "Encrypt the content of the current headline."
  89. (interactive)
  90. (require 'epg)
  91. (save-excursion
  92. (org-back-to-heading t)
  93. (let ((start-heading (point)))
  94. (forward-line)
  95. (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
  96. (let ((folded (org-invisible-p))
  97. (epg-context (epg-make-context nil t t))
  98. (crypt-key (org-crypt-key-for-heading))
  99. (beg (point))
  100. end encrypted-text)
  101. (goto-char start-heading)
  102. (org-end-of-subtree t t)
  103. (org-back-over-empty-lines)
  104. (setq end (point)
  105. encrypted-text
  106. (epg-encrypt-string
  107. epg-context
  108. (buffer-substring-no-properties beg end)
  109. (epg-list-keys epg-context crypt-key)))
  110. (delete-region beg end)
  111. (insert encrypted-text)
  112. (when folded
  113. (goto-char start-heading)
  114. (hide-subtree))
  115. nil)))))
  116. (defun org-decrypt-entry ()
  117. "Decrypt the content of the current headline."
  118. (interactive)
  119. (require 'epg)
  120. (save-excursion
  121. (org-back-to-heading t)
  122. (forward-line)
  123. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  124. (let* ((beg (point))
  125. (end (save-excursion
  126. (search-forward "-----END PGP MESSAGE-----")
  127. (forward-line)
  128. (point)))
  129. (epg-context (epg-make-context nil t t))
  130. (decrypted-text
  131. (decode-coding-string
  132. (epg-decrypt-string
  133. epg-context
  134. (buffer-substring-no-properties beg end))
  135. 'utf-8)))
  136. (delete-region beg end)
  137. (insert decrypted-text)
  138. nil))))
  139. (defun org-encrypt-entries ()
  140. "Encrypt all top-level entries in the current buffer."
  141. (interactive)
  142. (org-scan-tags
  143. 'org-encrypt-entry
  144. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  145. (defun org-decrypt-entries ()
  146. "Decrypt all entries in the current buffer."
  147. (interactive)
  148. (org-scan-tags
  149. 'org-decrypt-entry
  150. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  151. (defun org-crypt-use-before-save-magic ()
  152. "Adds a hook that will automatically encrypt entries before a
  153. file is saved to disk."
  154. (add-hook
  155. 'org-mode-hook
  156. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  157. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  158. (provide 'org-crypt)
  159. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  160. ;;; org-crypt.el ends here