all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Request for Comment, ELisp code
@ 2005-06-17  1:03 Denis Bueno
  2005-06-17  1:47 ` Joe Corneli
  2005-06-17 16:21 ` Johan Bockgård
  0 siblings, 2 replies; 3+ messages in thread
From: Denis Bueno @ 2005-06-17  1:03 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Request for Comment, ELisp code
  2005-06-17  1:03 Request for Comment, ELisp code Denis Bueno
@ 2005-06-17  1:47 ` Joe Corneli
  2005-06-17 16:21 ` Johan Bockgård
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Corneli @ 2005-06-17  1:47 UTC (permalink / raw)


Note: the typical destination for working code is gnu.emacs.sources.


   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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Request for Comment, ELisp code
  2005-06-17  1:03 Request for Comment, ELisp code Denis Bueno
  2005-06-17  1:47 ` Joe Corneli
@ 2005-06-17 16:21 ` Johan Bockgård
  1 sibling, 0 replies; 3+ messages in thread
From: Johan Bockgård @ 2005-06-17 16:21 UTC (permalink / raw)


Denis Bueno <dbuenoAOEUI@IUEOAgmail.com> writes:

>       (while args
>         (setq prog (concat prog " " (car args))
>               args (cdr args))))

You could use `dolist' instead of `while'. In this case using
`mapconcat' is even simpler though (use `identity' for FUNCTION).

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-06-17 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-17  1:03 Request for Comment, ELisp code Denis Bueno
2005-06-17  1:47 ` Joe Corneli
2005-06-17 16:21 ` Johan Bockgård

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.