From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Denis Bueno Newsgroups: gmane.emacs.help Subject: Request for Comment, ELisp code Date: Thu, 16 Jun 2005 21:03:35 -0400 Organization: Georgia Institute of Technology Message-ID: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1118970913 13508 80.91.229.2 (17 Jun 2005 01:15:13 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 17 Jun 2005 01:15:13 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 17 03:15:13 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Dj5RZ-00046T-8W for geh-help-gnu-emacs@m.gmane.org; Fri, 17 Jun 2005 03:14:49 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Dj5X5-0006fH-3E for geh-help-gnu-emacs@m.gmane.org; Thu, 16 Jun 2005 21:20:31 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!canoe.uoregon.edu!arclight.uoregon.edu!news-ext.gatech.edu!news-int.gatech.edu!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 115 Original-NNTP-Posting-Host: c-69-180-61-31.hsd1.ga.comcast.net Original-X-Trace: news-int2.gatech.edu 1118970847 9442 69.180.61.31 (17 Jun 2005 01:14:07 GMT) Original-X-Complaints-To: usenet@news-int2.gatech.edu Original-NNTP-Posting-Date: Fri, 17 Jun 2005 01:14:07 +0000 (UTC) User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin) Cancel-Lock: sha1:PBIFrAE6lkucg2LSxzves91Lvj4= Original-Xref: shelby.stanford.edu gnu.emacs.help:132044 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:27514 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:27514 All, I hacked up a bit of elisp to encrypt/decrypt a buffer of text using gpg in the backend [1]. I've hacked Common Lisp for a few years now, but, not elisp. So, I wonder if any veteran elisp hackers have any suggestions, pointers, etc. Just load the code and put some text in a buffer; then M-x cryptob-encrypt-buffer and provide a recipient; you should get a new buffer of ASCII-armored encrypted text. Same for decryption, except you must start with the ASCII-armored encrypted text, and provide the password. The code works as is ... but I'm sure it can be improved. (Caveat: I'm using OS X so the path to gpg is likely different in most other configurations.) Thanks. -Denis Remove dvorak'ian influence from From address. [1] (require 'cl) (defvar cryptob-gpg "/sw/bin/gpg") (defvar cryptob-gpg-encrypt-flags '("-e" "--batch" "--quiet" "-a" "-o" "-")) (defvar cryptob-gpg-decrypt-flags '("-d" "--batch" "--quiet" "--passphrase-fd" "0" "-")) (defun cryptob-encrypt-buffer (recip &optional buffer) "Encrypt the contents of a text buffer using gpg." (interactive "sRecipient: ") (save-excursion (with-current-buffer (or buffer (current-buffer)) (cryptob-encrypt-region (point-min) (point-max) recip)))) (defun cryptob-decrypt-buffer (&optional buffer) "Decrypt the contents of a text buffer using gpg." (interactive) (let ((passphrase (read-passwd "Passphrase: " nil nil))) (save-excursion (with-current-buffer (or buffer (current-buffer)) (cryptob-decrypt-region (point-min) (point-max) passphrase))))) (defun cryptob-encrypt-region (start end recip) (interactive "*r\nP") (let (prog) ;; construct gpg call into a string, `prog' (let ((args (append (list cryptob-gpg) (list "-r" recip) (copy-list cryptob-gpg-encrypt-flags)))) (while args (setq prog (concat prog " " (car args)) args (cdr args)))) ;; (message "start=%d end=%d prog+args=%s" start end prog) (let ((output-buffer (get-buffer-create "*cryptob-encrypted-output*"))) ;; clear any text in output-buffer (with-current-buffer output-buffer (setq buffer-read-only t) (let ((inhibit-read-only t)) (erase-buffer) (local-set-key "q" '(lambda () (interactive) (kill-buffer (get-buffer "*cryptob-encrypted-output*")))))) (let ((inhibit-read-only t)) (call-process-region start end shell-file-name nil ; delete output-buffer ; buffer t shell-command-switch prog)) (pop-to-buffer output-buffer t)))) (defun cryptob-decrypt-region (start end passphrase) (interactive "*r") (let (prog) ;; construct gpg call into a string, `prog' (let ((args (cons cryptob-gpg (copy-list cryptob-gpg-decrypt-flags)))) (while args (setq prog (concat prog " " (car args)) args (cdr args)))) ;; put passphrase as first thing in region ;; ** in case we try to decrypt from a buffer we made, inhibit ;; read-only setting on that buffer. (let ((inhibit-read-only t)) (goto-char start) (insert passphrase "\n") (setq end (+ end (1+ (length passphrase))))) ;; (message "start=%d end=%d prog+args=%s" start end prog) (let ((output-buffer (get-buffer-create "*cryptob-decrypted-output*"))) ;; clear any text in output-buffer (with-current-buffer output-buffer (setq buffer-read-only t) (let ((inhibit-read-only t)) (erase-buffer) (local-set-key "q" '(lambda () (interactive) (kill-buffer (get-buffer "*cryptob-encrypted-output*")))))) (let ((inhibit-read-only t)) (call-process-region start end shell-file-name nil ; delete output-buffer ; buffer t shell-command-switch prog) ;; erase passphrase from current buffer; is this the right way? (goto-char start) (kill-line 1)) (pop-to-buffer output-buffer t))))