org-crypt.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; org-crypt.el --- Public key encryption for org-mode entries
  2. ;; Copyright (C) 2007, 2009 Free Software Foundation, Inc.
  3. ;; Emacs Lisp Archive Entry
  4. ;; Filename: org-crypt.el
  5. ;; Version: 6.34c
  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. (forward-line)
  93. (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
  94. (let ((folded (org-invisible-p))
  95. (epg-context (epg-make-context nil t t))
  96. (crypt-key (org-crypt-key-for-heading))
  97. (beg (point))
  98. end encrypted-text)
  99. (org-end-of-subtree t t)
  100. (org-back-over-empty-lines)
  101. (setq end (point)
  102. encrypted-text
  103. (epg-encrypt-string
  104. epg-context
  105. (buffer-substring-no-properties beg end)
  106. (epg-list-keys epg-context crypt-key)))
  107. (delete-region beg end)
  108. (insert encrypted-text)
  109. (when folded
  110. (save-excursion
  111. (org-back-to-heading t)
  112. (hide-subtree)))
  113. nil))))
  114. (defun org-decrypt-entry ()
  115. (interactive)
  116. (require 'epg)
  117. (save-excursion
  118. (org-back-to-heading t)
  119. (forward-line)
  120. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  121. (let* ((beg (point))
  122. (end (save-excursion
  123. (search-forward "-----END PGP MESSAGE-----")
  124. (forward-line)
  125. (point)))
  126. (epg-context (epg-make-context nil t t))
  127. (decrypted-text
  128. (decode-coding-string
  129. (epg-decrypt-string
  130. epg-context
  131. (buffer-substring-no-properties beg end))
  132. 'utf-8)))
  133. (delete-region beg end)
  134. (insert decrypted-text)
  135. nil))))
  136. (defun org-encrypt-entries ()
  137. (interactive)
  138. (org-scan-tags
  139. 'org-encrypt-entry
  140. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  141. (defun org-decrypt-entries ()
  142. (interactive)
  143. (org-scan-tags
  144. 'org-decrypt-entry
  145. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  146. (defun org-crypt-use-before-save-magic ()
  147. "Adds a hook that will automatically encrypt entries before a
  148. file is saved to disk."
  149. (add-hook
  150. 'org-mode-hook
  151. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  152. (provide 'org-crypt)
  153. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  154. ;;; org-crypt.el ends here