Skip to content

Commit

Permalink
improve scrypt-p logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anticomputer committed Jan 2, 2023
1 parent 399e862 commit 055f7e2
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions age.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
;; Homepage: https://github.com/anticomputer/age.el
;; Package-Requires: ((emacs "28.1"))
;; Keywords: data
;; Version: 0.1.1
;; Version: 0.1.2

;; This is a port of epg.el and epa-file.el, original copyright applies:

Expand Down Expand Up @@ -66,7 +66,7 @@
(defconst age-package-name "age"
"Name of this package.")

(defconst age-version-number "0.1.1"
(defconst age-version-number "0.1.2"
"Version number of this package.")

;;; Options
Expand Down Expand Up @@ -345,29 +345,30 @@ question, and the callback data (if any)."

(defun age-scrypt-p (file)
"Check for passphrase scrypt stanza in age FILE."
(with-temp-buffer
;; disable age file handling for this insert, we just want to grab a header
(let ((file-name-handler-alist (remq age-file-handler file-name-handler-alist))
(auto-mode-alist (remq age-file-auto-mode-alist-entry auto-mode-alist)))
(insert-file-contents-literally file nil 0 100))
(let ((lines
;; grab the first two lines
(cl-loop repeat 2
unless (eobp)
collect
(prog1 (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))
(forward-line 1)))))
;; deal with empty/new files as well by checking for no lines
(when lines
;; if the first line is the ascii armor marker, base64 decode the second line
(let ((b64 (string-match-p
"-----BEGIN AGE ENCRYPTED FILE-----" (car lines)))
(l2 (cadr lines)))
;; if the second line contains the scrypt stanza, it is a passphrase file
(when (string-match-p "-> scrypt " (if b64 (base64-decode-string l2) l2))
t))))))
(when (file-exists-p (expand-file-name file))
(with-temp-buffer
;; disable age file handling for this insert, we just want to grab a header
(let ((file-name-handler-alist (remq age-file-handler file-name-handler-alist))
(auto-mode-alist (remq age-file-auto-mode-alist-entry auto-mode-alist)))
(insert-file-contents-literally file nil 0 100))
(let ((lines
;; grab the first two lines
(cl-loop repeat 2
unless (eobp)
collect
(prog1 (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))
(forward-line 1)))))
;; deal with empty/new files as well by checking for no lines
(when (and lines (= (length lines) 2))
;; if the first line is the ascii armor marker, base64 decode the second line
(let ((b64 (string-match-p
"-----BEGIN AGE ENCRYPTED FILE-----" (car lines)))
(l2 (cadr lines)))
;; if the second line contains the scrypt stanza, it is a passphrase file
(when (string-match-p "-> scrypt " (if b64 (base64-decode-string l2) l2))
t)))))))

(defun age-context-result-for (context name)
"Return the result of CONTEXT associated with NAME."
Expand Down

0 comments on commit 055f7e2

Please sign in to comment.