help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Request for Comment, ELisp code


From: Denis Bueno
Subject: Request for Comment, ELisp code
Date: Thu, 16 Jun 2005 21:03:35 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin)

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))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]