(require 'epg) (eval-when-compile (require 'pgg)) (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase) "This function is for internal use only. Encrypt the current region between START and END. If optional argument SIGN is non-nil, do a combined sign and encrypt. If optional PASSPHRASE is not specified, it will be obtained from the passphrase cache or user." (let ((context (epg-make-context)) cipher) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (setq cipher (epg-encrypt-string context (buffer-substring start end) (if pgg-encrypt-for-me (cons pgg-default-user-id recipients) recipients) sign t)) (save-excursion (set-buffer (get-buffer-create pgg-output-buffer)) (erase-buffer) (insert cipher)) t)) (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase) "This function is for internal use only. Encrypt the current region between START and END with symmetric cipher. If optional PASSPHRASE is not specified, it will be obtained from the passphrase cache or user." (pgg-epg-encrypt-region start end nil)) (defun pgg-epg-decrypt-region (start end &optional passphrase) "This function is for internal use only. Decrypt the current region between START and END. If optional PASSPHRASE is not specified, it will be obtained from the passphrase cache or user." (let ((context (epg-make-context)) plain) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (setq plain (epg-decrypt-string context (buffer-substring start end))) (save-excursion (set-buffer (get-buffer-create pgg-output-buffer)) (erase-buffer) (insert plain)) t)) (defun pgg-epg-sign-region (start end &optional cleartext passphrase) "This function is for internal use only. Make detached signature from text between START and END. If optional PASSPHRASE is not specified, it will be obtained from the passphrase cache or user." (let ((context (epg-make-context)) signature) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (setq signature (epg-sign-string context (buffer-substring start end) (if cleartext 'cleartext 'detached))) (save-excursion (set-buffer (get-buffer-create pgg-output-buffer)) (insert signature)) t)) (defvar pgg-epg-signature nil) (defun pgg-epg-verify-region (start end &optional signature) "This function is for internal use only. Verify region between START and END as the detached signature SIGNATURE." (let ((context (epg-make-context)) pointer) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (if signature (epg-verify-file context signature (buffer-substring start end)) (epg-verify-string context (buffer-substring start end))) (setq signature (reverse (epg-context-result-for context 'verify)) pointer signature) (save-excursion (set-buffer (get-buffer-create pgg-errors-buffer)) (make-local-variable 'pgg-epg-signature) (setq pgg-epg-signature (car signature)) (erase-buffer) (while pointer (insert (format "%s: %s %s %s\n" (epg-signature-status (car pointer)) (epg-signature-key-id (car pointer)) (epg-signature-user-id (car pointer)) (epg-signature-validity (car pointer)))) (setq pointer (cdr pointer)))) signature)) (defun pgg-epg-insert-key () "This function is for internal use only. Insert public key at point." (let ((context (epg-make-context)) pointer) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (insert (epg-export-keys context pgg-default-user-id)))) (defun pgg-epg-snarf-keys-region (start end) "This function is for internal use only. Add all public keys in region between START and END to the keyring." (let ((context (epg-make-context)) pointer) (epg-context-set-armor context t) (epg-context-set-textmode context pgg-text-mode) (epg-import-keys context (buffer-substring start end)))) (defun mml2015-gpg-extract-signature-details () (if pgg-epg-signature (let* ((expired (eq (epg-signature-status pgg-epg-signature) 'key-expired)) (signer (cons (epg-signature-key-id pgg-epg-signature) (epg-signature-user-id pgg-epg-signature))) (fprint (epg-signature-fingerprint pgg-epg-signature)) (trust-good-enough-p (memq (epg-signature-validity pgg-epg-signature) '(marginal fully ultimate)))) (cond ((and signer fprint) (concat (cdr signer) (unless trust-good-enough-p (concat "\nUntrusted, Fingerprint: " (mml2015-gpg-pretty-print-fpr fprint))) (when expired (format "\nWARNING: Signature from expired key (%s)" (car signer))))) (t "From unknown user"))) "From unknown user")) (provide 'pgg-epg) ;;; pgg-epg.el ends here