From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Wiegley Subject: org-crypt.el --- Public key encryption for org-mode entries Date: Fri, 28 Sep 2007 23:48:34 -0400 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IbTJt-0000Fz-KZ for emacs-orgmode@gnu.org; Fri, 28 Sep 2007 23:48:45 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IbTJs-0000FV-Is for emacs-orgmode@gnu.org; Fri, 28 Sep 2007 23:48:45 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IbTJs-0000FS-Cg for emacs-orgmode@gnu.org; Fri, 28 Sep 2007 23:48:44 -0400 Received: from johnwiegley.com ([208.70.150.153] helo=mail.johnwiegley.com) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1IbTJs-0004cO-0u for emacs-orgmode@gnu.org; Fri, 28 Sep 2007 23:48:44 -0400 Received: from Hermes.local (unknown [72.22.154.67]) by mail.johnwiegley.com (Postfix) with ESMTP id D9FF54222B0 for ; Fri, 28 Sep 2007 22:50:57 -0500 (CDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org The following code is preliminary, but gets the job done in my simple tests. Now's the time to beat down on, and refine, the user interface and behavior. John ;;; org-crypt.el --- Public key encryption for org-mode entries ;; Copyright (C) 2007 John Wiegley ;; Emacs Lisp Archive Entry ;; Filename: org-crypt.el ;; Version: 0.1 ;; Keywords: org-mode ;; Author: John Wiegley ;; Maintainer: John Wiegley ;; Description: Adds public key encryption to org-mode buffers ;; URL: http://www.newartisans.com/software/emacs.html ;; Compatibility: Emacs22 ;; This file is not part of GNU Emacs. ;; This is free software; you can redistribute it and/or modify it under ;; the terms of the GNU General Public License as published by the Free ;; Software Foundation; either version 2, or (at your option) any later ;; version. ;; ;; This is distributed in the hope that it will be useful, but WITHOUT ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ;; for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ;; MA 02111-1307, USA. ;;; Commentary: ;; Right now this is just a set of functions to play with. It depends on the ;; epg library. Here's how you would use it: ;; ;; 1. To mark an entry for encryption, use `M-x org-set-property' to set the ;; property CRYPTKEY to any address in your public keyring. The text of ;; the entry (but not its properties or headline) will be encrypted for ;; this user. For them to read it, the corresponding secret key must be ;; located in the secret key ring of the account where you try to decrypt ;; it. This makes it possible to leave secure notes that only the intended ;; recipient can read in a shared-org-mode-files scenario. ;; ;; 2. Next, at the top of your org-mode buffer, add this line: ;; ;; -*- mode: org; after-save-hook: (org-encrypt-entries) -*- ;; ;; This ensures that entries marked for encryption are encrypted whenever ;; the file is saved. If you want encryption to be manual, use `M-x ;; org-encrypt-entries'. Note that in this version -- mainly because I ;; don't know epg.el better -- you will be asked for your password for ;; every entry that needs encryption. ;; ;; 3. To later decrypt an entry, use `M-x org-decrypt-entry'. It might be ;; useful to bind this to a key, like C-c C-/. I hope that in the future, ;; C-c C-r can be might overloaded to also decrypt an entry if it's ;; encrypted, since that fits nicely with the meaning of "reveal". (require 'epg) (defun org-encrypt-entries () (interactive) (save-excursion (goto-char (point-min)) (while (not (eobp)) (outline-next-heading) (let* ((props (org-entry-properties)) (crypt-key (and props (cdr (assoc "CRYPTKEY" props))))) (when (and crypt-key (stringp crypt-key)) (forward-line) (unless (looking-at "-----BEGIN PGP MESSAGE-----") (let* ((begin (point)) (end (save-excursion (goto-char (car (org-get-property-block begin))) (forward-line -1) (point))) (epg-context (epg-make-context nil t t)) (encrypted-text (epg-encrypt-string epg-context (buffer-substring-no-properties begin end) (epg-list-keys epg-context crypt-key) t))) (delete-region begin end) (insert encrypted-text)))))))) (defun org-decrypt-entry () (interactive) (save-excursion (let* ((props (org-entry-properties)) (crypt-key (and props (cdr (assoc "CRYPTKEY" props))))) (when (and crypt-key (stringp crypt-key)) (org-back-to-heading t) (forward-line) (when (looking-at "-----BEGIN PGP MESSAGE-----") (let* ((begin (point)) (end (save-excursion (goto-char (car (org-get-property-block begin))) (forward-line -1) (point))) (epg-context (epg-make-context nil t t)) (decrypted-text (epg-decrypt-string epg-context (buffer-substring-no-properties begin end)))) (delete-region begin end) (insert decrypted-text))))))) (provide 'org-crypt) ;;; org-crypt.el ends here