org-crypt.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; org-crypt.el --- Public key encryption for org-mode entries
  2. ;; Copyright (C) 2009 Peter Jones <pjones@pmade.com>
  3. ;; Copyright (C) 2007 John Wiegley <johnw@gnu.org>
  4. ;; Emacs Lisp Archive Entry
  5. ;; Filename: org-crypt.el
  6. ;; Version: 6.31
  7. ;; Keywords: org-mode
  8. ;; Author: John Wiegley <johnw@gnu.org>
  9. ;; Maintainer: Peter Jones <pjones@pmade.com>
  10. ;; Description: Adds public key encryption to org-mode buffers
  11. ;; URL: http://www.newartisans.com/software/emacs.html
  12. ;; Compatibility: Emacs22
  13. ;; This file is part of GNU Emacs.
  14. ;;
  15. ;; GNU Emacs is free software: you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation, either version 3 of the License, or
  18. ;; (at your option) any later version.
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;; GNU General Public License for more details.
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  25. ;;; Commentary:
  26. ;; Right now this is just a set of functions to play with. It depends
  27. ;; on the epg library. Here's how you would use it:
  28. ;;
  29. ;; 1. To mark an entry for encryption, tag the heading with "crypt".
  30. ;; You can change the tag to any complex tag matching string by
  31. ;; setting the `org-crypt-tag-matcher' variable.
  32. ;;
  33. ;; 2. Set the encryption key to use in the `org-crypt-key' variable,
  34. ;; or use `M-x org-set-property' to set the property CRYPTKEY to
  35. ;; any address in your public keyring. The text of the entry (but
  36. ;; not its properties or headline) will be encrypted for this user.
  37. ;; For them to read it, the corresponding secret key must be
  38. ;; located in the secret key ring of the account where you try to
  39. ;; decrypt it. This makes it possible to leave secure notes that
  40. ;; only the intended recipient can read in a shared-org-mode-files
  41. ;; scenario.
  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. (error "No crypt key set"))))
  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. (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. (org-end-of-subtree t t)
  101. (org-back-over-empty-lines)
  102. (setq end (point)
  103. encrypted-text
  104. (epg-encrypt-string
  105. epg-context
  106. (buffer-substring-no-properties beg end)
  107. (epg-list-keys epg-context crypt-key)))
  108. (delete-region beg end)
  109. (insert encrypted-text)
  110. (when folded
  111. (save-excursion
  112. (org-back-to-heading t)
  113. (hide-subtree)))
  114. nil))))
  115. (defun org-decrypt-entry ()
  116. (interactive)
  117. (require 'epg)
  118. (save-excursion
  119. (org-back-to-heading t)
  120. (forward-line)
  121. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  122. (let* ((beg (point))
  123. (end (save-excursion
  124. (search-forward "-----END PGP MESSAGE-----")
  125. (forward-line)
  126. (point)))
  127. (epg-context (epg-make-context nil t t))
  128. (decrypted-text
  129. (decode-coding-string
  130. (epg-decrypt-string
  131. epg-context
  132. (buffer-substring-no-properties beg end))
  133. 'utf-8)))
  134. (delete-region beg end)
  135. (insert decrypted-text)
  136. nil))))
  137. (defun org-encrypt-entries ()
  138. (interactive)
  139. (org-scan-tags
  140. 'org-encrypt-entry
  141. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  142. (defun org-decrypt-entries ()
  143. (interactive)
  144. (org-scan-tags
  145. 'org-decrypt-entry
  146. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  147. (defun org-crypt-use-before-save-magic ()
  148. "Adds a hook that will automatically encrypt entries before a
  149. file is saved to disk."
  150. (add-hook
  151. 'org-mode-hook
  152. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  153. (provide 'org-crypt)
  154. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  155. ;;; org-crypt.el ends here