(require 'hex-util)
(defgroup sha1 nil
"Elisp interface for SHA1 hash computation."
:version "22.1"
:group 'extensions)
(defcustom sha1-maximum-internal-length 500
"*Maximum length of message to use Lisp version of SHA1 function.
If message is longer than this, `sha1-program' is used instead.
If this variable is set to 0, use external program only.
If this variable is set to nil, use internal function only."
:type 'integer
:group 'sha1)
(defcustom sha1-program '("sha1sum")
"*Name of program to compute SHA1.
It must be a string \(program name\) or list of strings \(name and its args\)."
:type '(repeat string)
:group 'sha1)
(defcustom sha1-use-external (condition-case ()
(executable-find (car sha1-program))
(error))
"*Use external SHA1 program.
If this variable is set to nil, use internal function only."
:type 'boolean
:group 'sha1)
(defun sha1-string-external (string &optional binary)
(let (prog args digest default-enable-multibyte-characters)
(if (consp sha1-program)
(setq prog (car sha1-program)
args (cdr sha1-program))
(setq prog sha1-program
args nil))
(with-temp-buffer
(insert string)
(apply (function call-process-region)
(point-min)(point-max)
prog t t nil args)
(setq digest (buffer-substring (point-min)(+ (point-min) 40))))
(if binary
(decode-hex-string digest)
digest)))
(defun sha1-region-external (beg end &optional binary)
(sha1-string-external (buffer-substring-no-properties beg end) binary))
(eval-when-compile
(defconst sha1-K0-high 23170) (defconst sha1-K0-low 31129) (defconst sha1-K1-high 28377) (defconst sha1-K1-low 60321) (defconst sha1-K2-high 36635) (defconst sha1-K2-low 48348) (defconst sha1-K3-high 51810) (defconst sha1-K3-low 49622)
(defmacro sha1-F0 (B C D)
(` (logxor (, D) (logand (, B) (logxor (, C) (, D))))))
(defmacro sha1-F1 (B C D)
(` (logxor (, B) (, C) (, D))))
(defmacro sha1-F2 (B C D)
(` (logior (logand (, B) (, C))
(logand (, D) (logior (, B) (, C))))))
(defmacro sha1-F3 (B C D)
(` (logxor (, B) (, C) (, D))))
(defmacro sha1-S1 (W-high W-low)
(` (let ((W-high (, W-high))
(W-low (, W-low)))
(setq S1W-high (+ (% (* W-high 2) 65536)
(/ W-low (, (/ 65536 2)))))
(setq S1W-low (+ (/ W-high (, (/ 65536 2)))
(% (* W-low 2) 65536))))))
(defmacro sha1-S5 (A-high A-low)
(` (progn
(setq S5A-high (+ (% (* (, A-high) 32) 65536)
(/ (, A-low) (, (/ 65536 32)))))
(setq S5A-low (+ (/ (, A-high) (, (/ 65536 32)))
(% (* (, A-low) 32) 65536))))))
(defmacro sha1-S30 (B-high B-low)
(` (progn
(setq S30B-high (+ (/ (, B-high) 4)
(* (% (, B-low) 4) (, (/ 65536 4)))))
(setq S30B-low (+ (/ (, B-low) 4)
(* (% (, B-high) 4) (, (/ 65536 4))))))))
(defmacro sha1-OP (round)
(` (progn
(sha1-S5 sha1-A-high sha1-A-low)
(sha1-S30 sha1-B-high sha1-B-low)
(setq sha1-A-low (+ ((, (intern (format "sha1-F%d" round)))
sha1-B-low sha1-C-low sha1-D-low)
sha1-E-low
(, (symbol-value
(intern (format "sha1-K%d-low" round))))
(aref block-low idx)
(progn
(setq sha1-E-low sha1-D-low)
(setq sha1-D-low sha1-C-low)
(setq sha1-C-low S30B-low)
(setq sha1-B-low sha1-A-low)
S5A-low)))
(setq carry (/ sha1-A-low 65536))
(setq sha1-A-low (% sha1-A-low 65536))
(setq sha1-A-high (% (+ ((, (intern (format "sha1-F%d" round)))
sha1-B-high sha1-C-high sha1-D-high)
sha1-E-high
(, (symbol-value
(intern (format "sha1-K%d-high" round))))
(aref block-high idx)
(progn
(setq sha1-E-high sha1-D-high)
(setq sha1-D-high sha1-C-high)
(setq sha1-C-high S30B-high)
(setq sha1-B-high sha1-A-high)
S5A-high)
carry)
65536)))))
(defmacro sha1-add-to-H (H X)
(` (progn
(setq (, (intern (format "sha1-%s-low" H)))
(+ (, (intern (format "sha1-%s-low" H)))
(, (intern (format "sha1-%s-low" X)))))
(setq carry (/ (, (intern (format "sha1-%s-low" H))) 65536))
(setq (, (intern (format "sha1-%s-low" H)))
(% (, (intern (format "sha1-%s-low" H))) 65536))
(setq (, (intern (format "sha1-%s-high" H)))
(% (+ (, (intern (format "sha1-%s-high" H)))
(, (intern (format "sha1-%s-high" X)))
carry)
65536)))))
)
(defvar sha1-H0-high)
(defvar sha1-H0-low)
(defvar sha1-H1-high)
(defvar sha1-H1-low)
(defvar sha1-H2-high)
(defvar sha1-H2-low)
(defvar sha1-H3-high)
(defvar sha1-H3-low)
(defvar sha1-H4-high)
(defvar sha1-H4-low)
(defun sha1-block (block-high block-low)
(let ( (sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
(sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
(sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
(sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
(sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
(idx 16))
(let ( S1W-high S1W-low)
(while (< idx 80)
(sha1-S1 (logxor (aref block-high (- idx 3))
(aref block-high (- idx 8))
(aref block-high (- idx 14))
(aref block-high (- idx 16)))
(logxor (aref block-low (- idx 3))
(aref block-low (- idx 8))
(aref block-low (- idx 14))
(aref block-low (- idx 16))))
(aset block-high idx S1W-high)
(aset block-low idx S1W-low)
(setq idx (1+ idx))))
(setq idx 0)
(let ( S5A-high S5A-low S30B-high S30B-low carry)
(while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
(while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
(while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
(while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
(let ( carry)
(sha1-add-to-H H0 A)
(sha1-add-to-H H1 B)
(sha1-add-to-H H2 C)
(sha1-add-to-H H3 D)
(sha1-add-to-H H4 E))))
(defun sha1-binary (string)
"Return the SHA1 of STRING in binary form."
(let ( (block-high (eval-when-compile (make-vector 80 nil)))
(block-low (eval-when-compile (make-vector 80 nil))))
(unwind-protect
(let* ( (len (length string))
(lim (* (/ len 64) 64))
(rem (% len 4))
(idx 0)(pos 0))
(setq sha1-H0-high 26437 sha1-H0-low 8961 sha1-H1-high 61389 sha1-H1-low 43913 sha1-H2-high 39098 sha1-H2-low 56574 sha1-H3-high 4146 sha1-H3-low 21622 sha1-H4-high 50130 sha1-H4-low 57840) (while (< pos lim)
(setq idx 0)
(while (< idx 16)
(aset block-high idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(setq pos (+ pos 2))
(aset block-low idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(setq pos (+ pos 2))
(setq idx (1+ idx)))
(sha1-block block-high block-low))
(if (prog1
(< (- len lim) 56)
(setq lim (- len rem))
(setq idx 0)
(while (< pos lim)
(aset block-high idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(setq pos (+ pos 2))
(aset block-low idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(setq pos (+ pos 2))
(setq idx (1+ idx)))
(cond
((= rem 3)
(aset block-high idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(setq pos (+ pos 2))
(aset block-low idx (+ (* (aref string pos) 256)
128)))
((= rem 2)
(aset block-high idx (+ (* (aref string pos) 256)
(aref string (1+ pos))))
(aset block-low idx 32768))
((= rem 1)
(aset block-high idx (+ (* (aref string pos) 256)
128))
(aset block-low idx 0))
(t (aset block-high idx 32768)
(aset block-low idx 0)))
(setq idx (1+ idx))
(while (< idx 16)
(aset block-high idx 0)
(aset block-low idx 0)
(setq idx (1+ idx))))
(progn
(aset block-low 15 (* (% len 8192) 8))
(setq len (/ len 8192))
(aset block-high 15 (% len 65536))
(sha1-block block-high block-low))
(sha1-block block-high block-low)
(fillarray block-high 0)
(fillarray block-low 0)
(aset block-low 15 (* (% len 8192) 8))
(setq len (/ len 8192))
(aset block-high 15 (% len 65536))
(sha1-block block-high block-low))
(let ((result (make-string 20 0)))
(aset result 0 (/ sha1-H0-high 256))
(aset result 1 (% sha1-H0-high 256))
(aset result 2 (/ sha1-H0-low 256))
(aset result 3 (% sha1-H0-low 256))
(aset result 4 (/ sha1-H1-high 256))
(aset result 5 (% sha1-H1-high 256))
(aset result 6 (/ sha1-H1-low 256))
(aset result 7 (% sha1-H1-low 256))
(aset result 8 (/ sha1-H2-high 256))
(aset result 9 (% sha1-H2-high 256))
(aset result 10 (/ sha1-H2-low 256))
(aset result 11 (% sha1-H2-low 256))
(aset result 12 (/ sha1-H3-high 256))
(aset result 13 (% sha1-H3-high 256))
(aset result 14 (/ sha1-H3-low 256))
(aset result 15 (% sha1-H3-low 256))
(aset result 16 (/ sha1-H4-high 256))
(aset result 17 (% sha1-H4-high 256))
(aset result 18 (/ sha1-H4-low 256))
(aset result 19 (% sha1-H4-low 256))
result))
(fillarray block-high nil)
(fillarray block-low nil))))
(defun sha1-string-internal (string &optional binary)
(if binary
(sha1-binary string)
(encode-hex-string (sha1-binary string))))
(defun sha1-region-internal (beg end &optional binary)
(sha1-string-internal (buffer-substring-no-properties beg end) binary))
(defun sha1-region (beg end &optional binary)
(if (and sha1-use-external
sha1-maximum-internal-length
(> (abs (- end beg)) sha1-maximum-internal-length))
(sha1-region-external beg end binary)
(sha1-region-internal beg end binary)))
(defun sha1-string (string &optional binary)
(if (and sha1-use-external
sha1-maximum-internal-length
(> (length string) sha1-maximum-internal-length))
(sha1-string-external string binary)
(sha1-string-internal string binary)))
(defun sha1 (object &optional beg end binary)
"Return the SHA1 (Secure Hash Algorithm) of an object.
OBJECT is either a string or a buffer.
Optional arguments BEG and END denote buffer positions for computing the
hash of a portion of OBJECT.
If BINARY is non-nil, return a string in binary form."
(if (stringp object)
(sha1-string object binary)
(save-excursion
(set-buffer object)
(sha1-region (or beg (point-min)) (or end (point-max)) binary))))
(provide 'sha1)