From: ueno Date: Thu, 25 May 2006 06:40:59 +0000 (+0000) Subject: * epa.el (epa-decrypt-armor-in-region): New command. X-Git-Tag: epg-0_0_2~37 X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=eeaa937a3de2d4fc177249e74d347727829ef78e;p=elisp%2Fepg.git * epa.el (epa-decrypt-armor-in-region): New command. (epa-verify-armor-in-region): New command. (epa-sign-region): New command. (epa-encrypt-region): New command. --- diff --git a/ChangeLog b/ChangeLog index e0f1fce..cda6f36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,8 +2,10 @@ * epa.el (epa-decrypt-file): Display verify result if the message is signed. - (epa-decrypt-region): New command. - (epa-verify-region): New command. + (epa-decrypt-armor-in-region): New command. + (epa-verify-armor-in-region): New command. + (epa-sign-region): New command. + (epa-encrypt-region): New command. 2006-05-11 Daiki Ueno diff --git a/epa.el b/epa.el index 6ebf0ac..12b5740 100644 --- a/epa.el +++ b/epa.el @@ -506,26 +506,30 @@ If ARG is non-nil, mark the current line." (epg-context-result-for context 'verify))))) ;;;###autoload -(defun epa-sign-file (file signers detached) - "Sign FILE by selected SIGNERS keys. -If DETACHED is non-nil, it creates a detached signature." +(defun epa-sign-file (file signers mode) + "Sign FILE by SIGNERS keys selected." (interactive (list (expand-file-name (read-file-name "File: ")) (epa-select-keys (epg-make-context) "Select keys for signing. If no one is selected, default secret key is used. " nil t) - (y-or-n-p "Make a detached signature? "))) - (let ((signature (concat file (if epa-armor - ".asc" - (if detached - ".sig" - ".gpg")))) + (if (y-or-n-p "Make a detached signature? ") + 'detached + (if (y-or-n-p "Make a cleartext signature? ") + 'clear)))) + (let ((signature (concat file + (if (or epa-armor + (not (memq mode '(nil t normal detached)))) + ".asc" + (if (memq mode '(t detached)) + ".sig" + ".gpg")))) (context (epg-make-context))) (epg-context-set-armor context epa-armor) (epg-context-set-textmode context epa-textmode) (message "Signing %s..." (file-name-nondirectory file)) (epg-context-set-signers context signers) - (epg-sign-file context file signature (not (null detached))) + (epg-sign-file context file signature mode) (message "Signing %s...done" (file-name-nondirectory file)))) ;;;###autoload @@ -544,6 +548,125 @@ If no one is selected, symmetric encryption will be performed. "))) (message "Encrypting %s...done" (file-name-nondirectory file)))) ;;;###autoload +(defun epa-decrypt-armor-in-region (start end) + "Decrypt OpenPGP armors in the current region between START and END." + (interactive "r") + (save-excursion + (save-restriction + (narrow-to-region start end) + (goto-char start) + (let (armor-start armor-end charset plain coding-system) + (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) + (setq armor-start (match-beginning 0) + armor-end (re-search-forward "^-----END PGP MESSAGE-----$" + nil t)) + (unless armor-end + (error "No armor tail")) + (goto-char armor-start) + (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) + (setq charset (match-string 1))) + (message "Decrypting...") + (setq plain (epg-decrypt-string + (epg-make-context) + (buffer-substring armor-start armor-end))) + (message "Decrypting...done") + (delete-region armor-start armor-end) + (goto-char armor-start) + (if coding-system-for-read + (setq coding-system coding-system-for-read) + (if charset + (setq coding-system (intern (downcase charset))) + (setq coding-system 'utf-8))) + (insert (decode-coding-string plain coding-system)) + (if (epg-context-result-for context 'verify) + (message "%s" + (epg-verify-result-to-string + (epg-context-result-for context 'verify))))))))) + +;;;###autoload +(defun epa-verify-armor-in-region (start end) + "Verify OpenPGP armors in the current region between START and END." + (interactive "r") + (save-excursion + (save-restriction + (narrow-to-region start end) + (goto-char start) + (let (armor-start armor-end) + (while (re-search-forward "-----BEGIN PGP\\( SIGNED\\)? MESSAGE-----$" + nil t) + (setq armor-start (match-beginning 0)) + (if (match-beginning 1) ;cleartext signed message + (progn + (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" + nil t) + (error "Invalid cleartext signed message")) + (setq armor-end (re-search-forward + "^-----END PGP SIGNATURE-----$" + nil t))) + (setq armor-end (re-search-forward + "^-----END PGP MESSAGE-----$" + nil t))) + (unless armor-end + (error "No armor tail")) + (epg-verify-string (epg-make-context) + (encode-coding-string + (buffer-substring armor-start armor-end) + coding-system-for-write)) + (message "%s" + (epg-verify-result-to-string + (epg-context-result-for context 'verify)))))))) + +;;;###autoload +(defun epa-sign-region (start end signers mode) + "Sign the current region between START and END by SIGNERS keys selected." + (interactive + (list (region-beginning) (region-end) + (epa-select-keys (epg-make-context) "Select keys for signing. +If no one is selected, default secret key is used. " + nil t) + (if (y-or-n-p "Make a detached signature? ") + 'detached + (if (y-or-n-p "Make a cleartext signature? ") + 'clear)))) + (save-excursion + (let ((context (epg-make-context)) + signature) + (epg-context-set-armor context epa-armor) + (epg-context-set-textmode context epa-textmode) + (message "Signing...") + (epg-context-set-signers context signers) + (setq signature (epg-sign-string context + (encode-coding-string + (buffer-substring start end) + coding-system-for-write) + mode)) + (message "Signing...done") + (delete-region start end) + (insert (decode-coding-string signature coding-system-for-read))))) + +;;;###autoload +(defun epa-encrypt-region (start end recipients) + "Encrypt the current region between START and END for RECIPIENTS." + (interactive + (list (region-beginning) (region-end) + (epa-select-keys (epg-make-context) "Select recipents for encryption. +If no one is selected, symmetric encryption will be performed. "))) + (save-excursion + (let ((context (epg-make-context)) + cipher) + (epg-context-set-armor context epa-armor) + (epg-context-set-textmode context epa-textmode) + (message "Encrypting...") + (setq cipher (epg-encrypt-string context + (encode-coding-string + (buffer-substring start end) + coding-system-for-write) + recipients)) + (message "Encrypting...done") + (delete-region start end) + (insert cipher)))) + +;;;###autoload (defun epa-delete-keys (keys &optional allow-secret) "Delete selected KEYS." (interactive @@ -609,75 +732,6 @@ If LOCAL is non-nil, the signature is marked as non exportable." (epg-sign-keys context keys local) (message "Signing keys...done"))) -;;;###autoload -(defun epa-decrypt-region (start end) - "Decrypt OpenPGP armors in the current region between START and END." - (interactive "r") - (save-excursion - (save-restriction - (narrow-to-region start end) - (goto-char start) - (let (armor-start armor-end charset context plain coding-system) - (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) - (setq armor-start (match-beginning 0) - armor-end (re-search-forward "^-----END PGP MESSAGE-----$" - nil t)) - (unless armor-end - (error "No armor tail")) - (goto-char armor-start) - (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) - (setq charset (match-string 1))) - (setq context (epg-make-context) - plain (epg-decrypt-string - context - (buffer-substring armor-start armor-end))) - (delete-region armor-start armor-end) - (goto-char armor-start) - (if coding-system-for-read - (setq coding-system coding-system-for-read) - (if charset - (setq coding-system (intern (downcase charset))) - (setq coding-system 'utf-8))) - (insert (decode-coding-string plain coding-system)) - (if (epg-context-result-for context 'verify) - (message "%s" - (epg-verify-result-to-string - (epg-context-result-for context 'verify))))))))) - -;;;###autoload -(defun epa-verify-region (start end) - "Verify OpenPGP armors in the current region between START and END." - (interactive "r") - (save-excursion - (save-restriction - (narrow-to-region start end) - (goto-char start) - (let (armor-start armor-end context) - (while (re-search-forward "-----BEGIN PGP\\( SIGNED\\)? MESSAGE-----$" - nil t) - (setq armor-start (match-beginning 0)) - (if (match-beginning 1) ;cleartext signed message - (progn - (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" - nil t) - (error "Invalid cleartext signed message")) - (setq armor-end (re-search-forward - "^-----END PGP SIGNATURE-----$" - nil t))) - (setq armor-end (re-search-forward - "^-----END PGP MESSAGE-----$" - nil t))) - (unless armor-end - (error "No armor tail")) - (setq context (epg-make-context)) - (epg-verify-string context - (encode-coding-string - (buffer-substring armor-start armor-end) - coding-system-for-write)) - (message "%s" - (epg-verify-result-to-string - (epg-context-result-for context 'verify)))))))) - (provide 'epa) ;;; epa.el ends here