1 ;;; epa-file.el --- the EasyPG Assistant hooks for transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of EasyPG.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
28 (defgroup epa-file nil
29 "The EasyPG Assistant hooks for transparent file encryption"
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33 "Regexp which matches filenames to be encrypted with GnuPG."
38 (defvar epa-passphrase nil)
40 (defun epa-file-passphrase-callback-function (key-id buffer)
43 (if (and (eq key-id 'SYM)
46 (let ((passphrase (epg-passphrase-callback-function
48 (setq epa-passphrase (copy-sequence passphrase))
50 (epg-passphrase-callback-function key-id buffer))))
52 (defvar last-coding-system-used)
53 (defun epa-find-file ()
54 (when (string-match epa-file-name-regexp (buffer-file-name))
55 (when (file-exists-p (expand-file-name (buffer-file-name)))
56 (make-local-variable 'epa-file)
57 (make-local-variable 'epa-passphrase)
58 (if (fboundp 'set-buffer-multibyte)
59 (set-buffer-multibyte t))
60 (goto-char (point-min))
61 (let ((context (epg-make-context)))
62 (epg-context-set-passphrase-callback
64 (cons #'epa-file-passphrase-callback-function
66 (insert (epg-decrypt-file context
67 (expand-file-name (buffer-file-name))
69 (delete-region (point) (point-max))
70 (decode-coding-region (point-min) (point-max) 'undecided)
71 (if (boundp 'last-coding-system-used)
72 (set-buffer-file-coding-system last-coding-system-used)
73 (set-buffer-file-coding-system default-buffer-file-coding-system))
75 (hack-local-variables)
77 (set-buffer-modified-p nil)
78 (setq buffer-undo-list nil
79 epa-file (buffer-file-name)))))
81 (defun epa-write-file ()
83 (let* ((coding-system (if (boundp 'last-coding-system-used)
85 (write-region (point-min) (point-max) "/")
86 (error last-coding-system-used))
87 buffer-file-coding-system))
88 (coding-system-for-write 'binary)
89 (context (epg-make-context)))
90 (epg-context-set-passphrase-callback
92 (cons #'epa-file-passphrase-callback-function
97 (encode-coding-string (buffer-string) coding-system)
99 (epg-sub-key-id (car (epg-key-sub-key-list key))))
100 (unless epa-passphrase
102 "Select recipents for encryption.
103 If no one is selected, symmetric encryption will be performed. "))))
104 nil (expand-file-name (buffer-file-name))))
105 (set-visited-file-modtime)
106 (set-buffer-modified-p nil)
111 ;;; epa-file.el ends here