From: okada Date: Mon, 22 Nov 1999 19:37:41 +0000 (+0000) Subject: Fix sasl.el hmac-util -> hex-util. X-Git-Tag: slim-1_13_3 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1fa04c43f0d42168eab0b09f91fe4e6702b6eff;p=elisp%2Fflim.git Fix sasl.el hmac-util -> hex-util. Update sha1. --- diff --git a/ChangeLog b/ChangeLog index 8cd64a2..3ef4533 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,39 @@ 1999-10-23 Kenichi OKADA + * SLIM: Version 1.13.3 released. + +1999-10-23 Tsunehiko Baba + + * sasl.el: Don't require `hmac-util'. + (sasl-cram-md5): Update to use encode-hex-string. + +1999-10-23 Kenichi OKADA + + * sha1.el: Update. + * sha1-el.el: Update. + * sha1-dl.el: Update. + * hmac-sha1.el: Update. + +1999-06-23 Shuhei KOBAYASHI + + * sha1-el.el (sha1-string-external): Don't use `with-temp-buffer'. + +1999-06-21 Shuhei KOBAYASHI + + * sha1-el.el: Added external program support. + (sha1-maximum-internal-length, sha1-program): New variables. + (sha1-string-external, sha1-region-external): New functions. + (sha1-region, sha1-string): Use above. + +1999-06-20 Shuhei KOBAYASHI + + * md5-dl.el (md5-region): Not interactive. + sha1-dl.el (sha1-region): Ditto. + sha1-el.el (sha1-region-internal): Ditto. + + +1999-10-23 Kenichi OKADA + * SLIM: Version 1.13.2 released. 1999-10-23 Kenichi OKADA diff --git a/SLIM-VERSION b/SLIM-VERSION index 308485f..be9c519 100644 --- a/SLIM-VERSION +++ b/SLIM-VERSION @@ -3,4 +3,4 @@ 1.13.0 藤原紀香 1.13.1 深田恭子 1.13.2 華原朋美 ------- 飯島直子 +1.13.3 飯島直子 diff --git a/hmac-sha1.el b/hmac-sha1.el index 30bb061..6b2beea 100644 --- a/hmac-sha1.el +++ b/hmac-sha1.el @@ -26,35 +26,35 @@ ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1". ;; -;; (hmac-hex-string (hmac-sha1 "Hi There" (make-string 20 ?\x0b))) +;; (encode-hex-string (hmac-sha1 "Hi There" (make-string 20 ?\x0b))) ;; => "b617318655057264e28bc0b6fb378c8ef146be00" ;; -;; (hmac-hex-string (hmac-sha1 "what do ya want for nothing?" "Jefe")) +;; (encode-hex-string (hmac-sha1 "what do ya want for nothing?" "Jefe")) ;; => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" ;; -;; (hmac-hex-string (hmac-sha1 (make-string 50 ?\xdd) (make-string 20 ?\xaa))) +;; (encode-hex-string (hmac-sha1 (make-string 50 ?\xdd) (make-string 20 ?\xaa))) ;; => "125d7342b9ac11cd91a39af48aa17b4f63f175d3" ;; -;; (hmac-hex-string +;; (encode-hex-string ;; (hmac-sha1 ;; (make-string 50 ?\xcd) -;; (hmac-unhex-string "0102030405060708090a0b0c0d0e0f10111213141516171819"))) +;; (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819"))) ;; => "4c9007f4026250c6bc8414f9bf50c86c2d7235da" ;; -;; (hmac-hex-string +;; (encode-hex-string ;; (hmac-sha1 "Test With Truncation" (make-string 20 ?\x0c))) ;; => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04" -;; (hmac-hex-string +;; (encode-hex-string ;; (hmac-sha1-96 "Test With Truncation" (make-string 20 ?\x0c))) ;; => "4c1a03424b55e07fe7f27be1" ;; -;; (hmac-hex-string +;; (encode-hex-string ;; (hmac-sha1 ;; "Test Using Larger Than Block-Size Key - Hash Key First" ;; (make-string 80 ?\xaa))) ;; => "aa4ae5e15272d00e95705637ce8a3b55ed402112" ;; -;; (hmac-hex-string +;; (encode-hex-string ;; (hmac-sha1 ;; "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" ;; (make-string 80 ?\xaa))) @@ -63,12 +63,18 @@ ;;; Code: (eval-when-compile (require 'hmac-def)) +(require 'hex-util) ; (decode-hex-string STRING) (require 'sha1) ; expects (sha1 STRING) -(define-hmac-function hmac-sha1 sha1 64 20) ; => (hmac-sha1 TEXT KEY) -;; (define-hmac-function hmac-sha1-96 sha1 64 20 96) -;; => (hmac-sha1-96 TEXT KEY) +;;; For consintency with hmac-md5.el, we define this function here. +(or (fboundp 'sha1-binary) + (defun sha1-binary (string) + "Return the SHA1 of STRING in binary form." + (decode-hex-string (sha1 string)))) + +(define-hmac-function hmac-sha1 sha1-binary 64 20) ; => (hmac-sha1 TEXT KEY) +;; (define-hmac-function hmac-sha1-96 sha1-binary 64 20 96) (provide 'hmac-sha1) -;;; hmac-sha1.el ends here. +;;; hmac-sha1.el ends here diff --git a/mime-def.el b/mime-def.el index 746fcc2..7eb26f6 100644 --- a/mime-def.el +++ b/mime-def.el @@ -35,7 +35,7 @@ (eval-when-compile (require 'cl)) ; list* (eval-and-compile - (defconst mime-library-product ["SLIM" (1 13 2) "華原朋美"] + (defconst mime-library-product ["SLIM" (1 13 3) "飯島直子"] "Product name, version number and code name of MIME-library package.") ) diff --git a/sasl.el b/sasl.el index 66ac188..f9d001f 100644 --- a/sasl.el +++ b/sasl.el @@ -25,11 +25,10 @@ ;;; Code: (require 'hmac-md5) -(require 'hmac-util) (defun sasl-cram-md5 (user passphrase response) (concat user " " - (hmac-hex-string + (encode-hex-string (hmac-md5 response passphrase)))) (defun sasl-plain (authorid authenid passphrase) @@ -37,4 +36,4 @@ (provide 'sasl) -;;; smtp.el ends here +;;; sasl.el ends here diff --git a/sha1-dl.el b/sha1-dl.el index c103cd8..06f51a2 100644 --- a/sha1-dl.el +++ b/sha1-dl.el @@ -26,28 +26,20 @@ ;;; Code: -(defvar sha1-dl-module - (if (and (fboundp 'sha1-string) - (subrp (symbol-function 'sha1-string))) - nil - (if (fboundp 'dynamic-link) - (let ((path (expand-file-name "sha1.so" exec-directory))) - (and (file-exists-p path) - path))))) +(provide 'sha1-dl) ; beware of circular dependency. +(eval-when-compile (require 'sha1)) ; sha1-dl-module. (defvar sha1-dl-handle (and (stringp sha1-dl-module) (file-exists-p sha1-dl-module) (dynamic-link sha1-dl-module))) -;;; sha1-dl-module provides `sha1-string'. +;;; sha1-dl-module provides `sha1-string' and `sha1-binary'. (dynamic-call "emacs_sha1_init" sha1-dl-handle) (defun sha1-region (beg end) - (interactive "r") (sha1-string (buffer-substring-no-properties beg end))) -;;;###autoload (defun sha1 (object &optional beg end) "Return the SHA1 (Secure Hash Algorithm) of an object. OBJECT is either a string or a buffer. @@ -60,6 +52,5 @@ hash of a portion of OBJECT." (sha1-region (or beg (point-min)) (or end (point-max)))))) (provide 'sha1-dl) -(provide 'sha1) -;;; sha1-dl.el ends here. +;;; sha1-dl.el ends here diff --git a/sha1-el.el b/sha1-el.el index c2f96a9..1ff999f 100644 --- a/sha1-el.el +++ b/sha1-el.el @@ -1,399 +1,408 @@ -;;; sha1.el --- SHA1 Message Digest Algorithm. -;; Copyright (C) 1998,1999 Keiichi Suzuki. +;;; sha1-el.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp. -;; Author: Keiichi Suzuki -;; Author: Katsumi Yamaoka -;; Created: 1998-12-25 -;; Revised: 1999-01-13 -;; Keywords: sha1, news, cancel-lock, hmac, rfc2104 +;; Copyright (C) 1999 Shuhei KOBAYASHI + +;; Author: Shuhei KOBAYASHI +;; Keywords: SHA1, FIPS 180-1 ;; This file is part of FLIM (Faithful Library about Internet Message). -;; This program is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 2, or (at your option) -;; any later version. +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation; either version 2, or +;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. -;;; A copy of the GNU General Public License can be obtained from this -;;; program's author (send electronic mail to kyle@uunet.uu.net) or from -;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA -;;; 02139, USA. +;; You should have received a copy of the GNU General Public License +;; along with this program; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. ;;; Commentary: -;; This is a direct translation into Emacs LISP of the reference C -;; implementation of the SHA1 message digest algorithm. - -;;; Usage: - -;; To compute the SHA1 message digest for a message M (represented as -;; a string), call -;; -;; (sha1-encode M) +;; This program is implemented from the definition of SHA-1 in FIPS PUB +;; 180-1 (Federal Information Processing Standards Publication 180-1), +;; "Announcing the Standard for SECURE HASH STANDARD". +;; +;; EXCEPTION: +;; * Two optimizations taken from GnuPG/cipher/sha1.c. ;; -;; which returns the message digest as a hexadecimal string of 20 bytes. -;; If you need to supply the message in pieces M1, M2, ... Mn, then call -;; -;; (sha1-init) -;; (sha1-update M1) -;; (sha1-update M2) -;; ... -;; (sha1-update Mn) -;; (sha1-final) - -;;; Notes: - -;; The C algorithm uses 32-bit integers; because GNU Emacs -;; implementations provide 28-bit integers (with 24-bit integers on -;; versions prior to 19.29), the code represents a 32-bit integer as the -;; cons of two 16-bit integers. The most significant word is stored in -;; the car and the least significant in the cdr. The algorithm requires -;; at least 19 bits of integer representation in order to represent the -;; carry from a 16-bit addition. (see sha1-add()) +;; BUGS: +;; * It is assumed that length of input string is less than 2^29 bytes. +;; * It is caller's responsibility to make string (or region) unibyte. ;;; Code: -(defmacro sha1-f1 (x y z) - `(cons - (logior (logand (car ,x) (car ,y)) (logand (lognot (car ,x)) (car ,z))) - (logior (logand (cdr ,x) (cdr ,y)) (logand (lognot (cdr ,x)) (cdr ,z))) - )) - -(defmacro sha1-f2 (x y z) - `(cons - (logxor (car ,x) (car ,y) (car ,z)) - (logxor (cdr ,x) (cdr ,y) (cdr ,z)) - )) - -(defmacro sha1-f3 (x y z) - `(cons - (logior (logand (car ,x) (car ,y)) (logand (car ,x) (car ,z)) - (logand (car ,y) (car ,z))) - (logior (logand (cdr ,x) (cdr ,y)) (logand (cdr ,x) (cdr ,z)) - (logand (cdr ,y) (cdr ,z))) - )) - -(defmacro sha1-f4 (x y z) - `(cons - (logxor (car ,x) (car ,y) (car ,z)) - (logxor (cdr ,x) (cdr ,y) (cdr ,z)) - )) - -(defconst sha1-const1 '(23170 . 31129) - "SHA constants 1 \(0x5a827999\)") -(defconst sha1-const2 '(28377 . 60321) - "SHA constants 2 \(0x6ed9eba1\)") -(defconst sha1-const3 '(36635 . 48348) - "SHA constants 3 \(0x8f1bbcdc\)") -(defconst sha1-const4 '(51810 . 49622) - "SHA constants 4 \(0xca62c1d6\)") - -(defvar sha1-digest (make-vector 5 nil)) -(defvar sha1-count-lo nil) -(defvar sha1-count-hi nil) -(defvar sha1-data nil) -(defvar sha1-local nil) -(defconst SHA1-BLOCKSIZE 64) - -(defun sha1-init () - "Initialize the state of the SHA1 message digest routines." - (aset sha1-digest 0 (cons 26437 8961)) - (aset sha1-digest 1 (cons 61389 43913)) - (aset sha1-digest 2 (cons 39098 56574)) - (aset sha1-digest 3 (cons 4146 21622)) - (aset sha1-digest 4 (cons 50130 57840)) - (setq sha1-count-lo (cons 0 0) - sha1-count-hi (cons 0 0) - sha1-local 0 - sha1-data nil) - ) - -(defmacro sha1-32-make (v) - "Return 32bits internal value from normal integer." - `(cons (lsh ,v -16) (logand 65535 ,v))) - -(defun sha1-add (to &rest vals) - "Set sum of all the arguments to the first one." - (let (val) - (while (setq val (car vals)) - (setcar to (+ (car to) (car val))) - (setcdr to (+ (cdr to) (cdr val))) - (setq vals (cdr vals)) - ) - (setcar to (logand 65535 (+ (car to) (lsh (cdr to) -16)))) - (setcdr to (logand 65535 (cdr to))) - to - )) - -(defun sha1-xor (to &rest vals) - "Set bitwise-exclusive-or of all the arguments to the first one." - (let (val) - (while (setq val (car vals)) - (setcar to (logxor (car to) (car val))) - (setcdr to (logxor (cdr to) (cdr val))) - (setq vals (cdr vals))) - )) - -(defmacro sha1-rot (val c1 c2) - "Internal macro for sha1-rot-*." - `(cons - (logand 65535 (logior (lsh (car ,val) ,c1) (lsh (cdr ,val) ,c2))) - (logand 65535 (logior (lsh (cdr ,val) ,c1) (lsh (car ,val) ,c2))) - )) - -(defmacro sha1-rot-1 (val) - "Return VAL with its bits rotated left by 1." - `(sha1-rot ,val 1 -15) - ) - -(defmacro sha1-rot-5 (val) - "Return VAL with its bits rotated left by 5." - `(sha1-rot ,val 5 -11) - ) - -(defmacro sha1-rot-30 (val) - "Return VAL with its bits rotated left by 30." - `(sha1-rot ,val -2 14) - ) - -(defun sha1-inc (to) - "Set TO pulus one to TO." - (setcdr to (1+ (cdr to))) - (when (> (cdr to) 65535) - (setcdr to (logand 65535 (cdr to))) - (setcar to (logand 65535 (1+ (car to)))))) - -(defun sha1-lsh (to v count) - "Set TO with its bits shifted left by COUNT to TO." - (setcar to (logand 65535 - (logior (lsh (car v) count) (lsh (cdr v) (- count 16))))) - (setcdr to (logand 65535 (lsh (cdr v) count))) - to - ) - -(defun sha1-rsh (to v count) - "Set TO with its bits shifted right by COUNT to TO." - (setq count (- 0 count)) - (setcdr to (logand 65535 - (logior (lsh (cdr v) count) (lsh (car v) (- count 16))))) - (setcar to (logand 65535 (lsh (car v) count))) - to - ) - -(defun sha1-< (v1 v2) - "Return t if firast argment is less then second argument." - (or (< (car v1) (car v2)) - (and (eq (car v1) (car v2)) - (< (cdr v1) (cdr v2)))) - ) - -(unless (fboundp 'string-as-unibyte) - (defsubst string-as-unibyte (string) - string) +(require 'hex-util) + +;;; +;;; external SHA1 function. +;;; + +(defvar 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 extarnal program only. +If this variable is set to nil, use internal function only.") + +(defvar sha1-program '("openssl" "sha1") + "*Name of program to compute SHA1. +It must be a string \(program name\) or list of strings \(name and its args\).") + +(defun sha1-string-external (string) + ;; `with-temp-buffer' is new in v20, so we do not use it. + (save-excursion + (let (buffer) + (unwind-protect + (let (prog args) + (if (consp sha1-program) + (setq prog (car sha1-program) + args (cdr sha1-program)) + (setq prog sha1-program + args nil)) + (setq buffer (set-buffer + (generate-new-buffer " *sha1 external*"))) + (insert string) + (apply (function call-process-region) + (point-min)(point-max) + prog t t nil args) + ;; SHA1 is 40 bytes long in hexadecimal form. + (buffer-substring (point-min)(+ (point-min) 40))) + (and buffer + (buffer-name buffer) + (kill-buffer buffer)))))) + +(defun sha1-region-external (beg end) + (sha1-string-external (buffer-substring-no-properties beg end))) + +;;; +;;; internal SHA1 function. +;;; + +(eval-when-compile + ;; optional second arg of string-to-number is new in v20. + (defconst sha1-K0-high 23170) ; (string-to-number "5A82" 16) + (defconst sha1-K0-low 31129) ; (string-to-number "7999" 16) + (defconst sha1-K1-high 28377) ; (string-to-number "6ED9" 16) + (defconst sha1-K1-low 60321) ; (string-to-number "EBA1" 16) + (defconst sha1-K2-high 36635) ; (string-to-number "8F1B" 16) + (defconst sha1-K2-low 48348) ; (string-to-number "BCDC" 16) + (defconst sha1-K3-high 51810) ; (string-to-number "CA62" 16) + (defconst sha1-K3-low 49622) ; (string-to-number "C1D6" 16) + +;;;; original definition of sha1-F0. +;;;; (defmacro sha1-F0 (B C D) +;;;; (` (logior (logand (, B) (, C)) +;;;; (logand (lognot (, B)) (, D))))) +;;;; a little optimization from GnuPG/cipher/sha1.c. + (defmacro sha1-F0 (B C D) + (` (logxor (, D) (logand (, B) (logxor (, C) (, D)))))) + (defmacro sha1-F1 (B C D) + (` (logxor (, B) (, C) (, D)))) +;;;; original definition of sha1-F2. +;;;; (defmacro sha1-F2 (B C D) +;;;; (` (logior (logand (, B) (, C)) +;;;; (logand (, B) (, D)) +;;;; (logand (, C) (, D))))) +;;;; a little optimization from GnuPG/cipher/sha1.c. + (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))))) ) -(defun sha1-update (bytes) - "Update the current SHA1 state with BYTES (an string of uni-bytes)." - (setq bytes (string-as-unibyte bytes)) - (let* ((len (length bytes)) - (len32 (sha1-32-make len)) - (tmp32 (cons 0 0)) - (top 0) - (clo (cons 0 0)) - i done) - (sha1-add clo sha1-count-lo (sha1-lsh tmp32 len32 3)) - (when (sha1-< clo sha1-count-lo) - (sha1-inc sha1-count-hi)) - (setq sha1-count-lo clo) - (sha1-add sha1-count-hi (sha1-rsh tmp32 len32 29)) - (when (> (length sha1-data) 0) - (setq i (- SHA1-BLOCKSIZE (length sha1-data))) - (when (> i len) - (setq i len)) - (setq sha1-data (concat sha1-data (substring bytes 0 i))) - (setq len (- len i) - top i) - (if (eq (length sha1-data) SHA1-BLOCKSIZE) - (sha1-transform) - (setq done t))) - (when (not done) - (while (and (not done) - (>= len SHA1-BLOCKSIZE)) - (setq sha1-data (substring bytes top (+ top SHA1-BLOCKSIZE)) - top (+ top SHA1-BLOCKSIZE) - len (- len SHA1-BLOCKSIZE)) - (sha1-transform)) - (setq sha1-data (substring bytes top (+ top len)))) - )) - -(defmacro sha1-FA (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq T (sha1-add (cons 0 0) (sha1-rot-5 A) (,func B C D) E (aref W WIDX) - ,const) - WIDX (1+ WIDX) - B (sha1-rot-30 B)))) - -(defmacro sha1-FB (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq E (sha1-add (cons 0 0) (sha1-rot-5 T) (,func A B C) D (aref W WIDX) - ,const) - WIDX (1+ WIDX) - A (sha1-rot-30 A)))) - -(defmacro sha1-FC (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq D (sha1-add (cons 0 0) (sha1-rot-5 E) (,func T A B) C (aref W WIDX) - ,const) - WIDX (1+ WIDX) - T (sha1-rot-30 T)))) - -(defmacro sha1-FD (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq C (sha1-add (cons 0 0) (sha1-rot-5 D) (,func E T A) B (aref W WIDX) - ,const) - WIDX (1+ WIDX) - E (sha1-rot-30 E)))) - -(defmacro sha1-FE (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq B (sha1-add (cons 0 0) (sha1-rot-5 C) (,func D E T) A (aref W WIDX) - ,const) - WIDX (1+ WIDX) - D (sha1-rot-30 D)))) - -(defmacro sha1-FT (n) - (let ((func (intern (format "sha1-f%d" n))) - (const (intern (format "sha1-const%d" n)))) - `(setq A (sha1-add (cons 0 0) (sha1-rot-5 B) (,func C D E) T (aref W WIDX) - ,const) - WIDX (1+ WIDX) - C (sha1-rot-30 C)))) - -(defun sha1-transform () - "Basic SHA1 step. Transform sha1-digest based on sha1-data." - (let ((W (make-vector 80 nil)) - (WIDX 0) - (bidx 0) - T A B C D E) - (while (< WIDX 16) - (aset W WIDX - (cons (logior (lsh (aref sha1-data bidx) 8) - (aref sha1-data (setq bidx (1+ bidx)))) - (logior (lsh (aref sha1-data (setq bidx (1+ bidx))) 8) - (aref sha1-data (setq bidx (1+ bidx)))))) - (setq bidx (1+ bidx) - WIDX (1+ WIDX))) - (while (< WIDX 80) - (aset W WIDX (cons 0 0)) - (sha1-xor (aref W WIDX) - (aref W (- WIDX 3)) (aref W (- WIDX 8)) - (aref W (- WIDX 14)) (aref W (- WIDX 16))) - (aset W WIDX (sha1-rot-1 (aref W WIDX))) - (setq WIDX (1+ WIDX))) - (setq A (cons (car (aref sha1-digest 0)) (cdr (aref sha1-digest 0))) - B (cons (car (aref sha1-digest 1)) (cdr (aref sha1-digest 1))) - C (cons (car (aref sha1-digest 2)) (cdr (aref sha1-digest 2))) - D (cons (car (aref sha1-digest 3)) (cdr (aref sha1-digest 3))) - E (cons (car (aref sha1-digest 4)) (cdr (aref sha1-digest 4))) - WIDX 0) - - (sha1-FA 1) (sha1-FB 1) (sha1-FC 1) (sha1-FD 1) (sha1-FE 1) (sha1-FT 1) - (sha1-FA 1) (sha1-FB 1) (sha1-FC 1) (sha1-FD 1) (sha1-FE 1) (sha1-FT 1) - (sha1-FA 1) (sha1-FB 1) (sha1-FC 1) (sha1-FD 1) (sha1-FE 1) (sha1-FT 1) - (sha1-FA 1) (sha1-FB 1) (sha1-FC 2) (sha1-FD 2) (sha1-FE 2) (sha1-FT 2) - (sha1-FA 2) (sha1-FB 2) (sha1-FC 2) (sha1-FD 2) (sha1-FE 2) (sha1-FT 2) - (sha1-FA 2) (sha1-FB 2) (sha1-FC 2) (sha1-FD 2) (sha1-FE 2) (sha1-FT 2) - (sha1-FA 2) (sha1-FB 2) (sha1-FC 2) (sha1-FD 2) (sha1-FE 3) (sha1-FT 3) - (sha1-FA 3) (sha1-FB 3) (sha1-FC 3) (sha1-FD 3) (sha1-FE 3) (sha1-FT 3) - (sha1-FA 3) (sha1-FB 3) (sha1-FC 3) (sha1-FD 3) (sha1-FE 3) (sha1-FT 3) - (sha1-FA 3) (sha1-FB 3) (sha1-FC 3) (sha1-FD 3) (sha1-FE 3) (sha1-FT 3) - (sha1-FA 4) (sha1-FB 4) (sha1-FC 4) (sha1-FD 4) (sha1-FE 4) (sha1-FT 4) - (sha1-FA 4) (sha1-FB 4) (sha1-FC 4) (sha1-FD 4) (sha1-FE 4) (sha1-FT 4) - (sha1-FA 4) (sha1-FB 4) (sha1-FC 4) (sha1-FD 4) (sha1-FE 4) (sha1-FT 4) - (sha1-FA 4) (sha1-FB 4) - - (sha1-add (aref sha1-digest 0) E) - (sha1-add (aref sha1-digest 1) T) - (sha1-add (aref sha1-digest 2) A) - (sha1-add (aref sha1-digest 3) B) - (sha1-add (aref sha1-digest 4) C) - )) - -(defun sha1-final (&optional binary) - "Transform buffered sha1-data and return SHA1 message digest. -If optional argument BINARY is non-nil, then return binary formed -string of message digest." - (let ((count (logand (lsh (cdr sha1-count-lo) -3) 63))) - (when (< (length sha1-data) SHA1-BLOCKSIZE) - (setq sha1-data - (concat sha1-data - (make-string (- SHA1-BLOCKSIZE (length sha1-data)) 0)))) - (aset sha1-data count 128) - (setq count (1+ count)) - (if (> count (- SHA1-BLOCKSIZE 8)) - (progn - (setq sha1-data (concat (substring sha1-data 0 count) - (make-string (- SHA1-BLOCKSIZE count) 0))) - (sha1-transform) - (setq sha1-data (concat (make-string (- SHA1-BLOCKSIZE 8) 0) - (substring sha1-data -8)))) - (setq sha1-data (concat (substring sha1-data 0 count) - (make-string (- SHA1-BLOCKSIZE 8 count) 0) - (substring sha1-data -8)))) - (aset sha1-data 56 (lsh (car sha1-count-hi) -8)) - (aset sha1-data 57 (logand 255 (car sha1-count-hi))) - (aset sha1-data 58 (lsh (cdr sha1-count-hi) -8)) - (aset sha1-data 59 (logand 255 (cdr sha1-count-hi))) - (aset sha1-data 60 (lsh (car sha1-count-lo) -8)) - (aset sha1-data 61 (logand 255 (car sha1-count-lo))) - (aset sha1-data 62 (lsh (cdr sha1-count-lo) -8)) - (aset sha1-data 63 (logand 255 (cdr sha1-count-lo))) - (sha1-transform) - (if binary - (mapconcat - (lambda (elem) - (concat (char-to-string (/ (car elem) 256)) - (char-to-string (% (car elem) 256)) - (char-to-string (/ (cdr elem) 256)) - (char-to-string (% (cdr elem) 256)))) - (list (aref sha1-digest 0) (aref sha1-digest 1) (aref sha1-digest 2) - (aref sha1-digest 3) (aref sha1-digest 4)) - "") - (format "%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x" - (car (aref sha1-digest 0)) (cdr (aref sha1-digest 0)) - (car (aref sha1-digest 1)) (cdr (aref sha1-digest 1)) - (car (aref sha1-digest 2)) (cdr (aref sha1-digest 2)) - (car (aref sha1-digest 3)) (cdr (aref sha1-digest 3)) - (car (aref sha1-digest 4)) (cdr (aref sha1-digest 4))) - ))) - -(defun sha1-encode (message &optional binary) - "Encodes MESSAGE using the SHA1 message digest algorithm. -MESSAGE must be a unibyte-string. -By default, return a string which formed hex-decimal charcters -from message digest. -If optional argument BINARY is non-nil, then return binary formed -string of message digest." - (sha1-init) - (sha1-update message) - (sha1-final binary)) - -(defun sha1-encode-binary (message) - "Encodes MESSAGE using the SHA1 message digest algorithm. -MESSAGE must be a unibyte-string. -Return binary formed string of message digest." - (sha1-encode message 'binary)) - -(provide 'sha1) - -;;; sha1.el ends here +;;; buffers (H0 H1 H2 H3 H4). +(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 (;; step (c) --- initialize buffers (A B C D E). + (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)) + ;; step (b). + (let (;; temporary variables used in sha1-S1 macro. + 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)))) + ;; step (d). + (setq idx 0) + (let (;; temporary variables used in sha1-OP macro. + 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)))) + ;; step (e). + (let (;; temporary variables used in sha1-add-to-H macro. + 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 (;; prepare buffers for a block. byte-length of block is 64. + ;; input block is split into two vectors. + ;; + ;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ... + ;; block-high: +-0-+ +-1-+ +-2-+ +-3-+ + ;; block-low: +-0-+ +-1-+ +-2-+ +-3-+ + ;; + ;; length of each vector is 80, and elements of each vector are + ;; 16bit integers. elements 0x10-0x4F of each vector are + ;; assigned later in `sha1-block'. + (block-high (eval-when-compile (make-vector 80 nil))) + (block-low (eval-when-compile (make-vector 80 nil)))) + (unwind-protect + (let* (;; byte-length of input string. + (len (length string)) + (lim (* (/ len 64) 64)) + (rem (% len 4)) + (idx 0)(pos 0)) + ;; initialize buffers (H0 H1 H2 H3 H4). + (setq sha1-H0-high 26437 ; (string-to-number "6745" 16) + sha1-H0-low 8961 ; (string-to-number "2301" 16) + sha1-H1-high 61389 ; (string-to-number "EFCD" 16) + sha1-H1-low 43913 ; (string-to-number "AB89" 16) + sha1-H2-high 39098 ; (string-to-number "98BA" 16) + sha1-H2-low 56574 ; (string-to-number "DCFE" 16) + sha1-H3-high 4146 ; (string-to-number "1032" 16) + sha1-H3-low 21622 ; (string-to-number "5476" 16) + sha1-H4-high 50130 ; (string-to-number "C3D2" 16) + sha1-H4-low 57840) ; (string-to-number "E1F0" 16) + ;; loop for each 64 bytes block. + (while (< pos lim) + ;; step (a). + (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)) + ;; last block. + (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))) + ;; this is the last (at most) 32bit word. + (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 ;; (= rem 0) + (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)))) + ;; last block has enough room to write the length of string. + (progn + ;; write bit length of string to last 4 bytes of the block. + (aset block-low 15 (* (% len 8192) 8)) + (setq len (/ len 8192)) + (aset block-high 15 (% len 65536)) + ;; XXX: It is not practical to compute SHA1 of + ;; such a huge message on emacs. + ;; (setq len (/ len 65536)) ; for 64bit emacs. + ;; (aset block-low 14 (% len 65536)) + ;; (aset block-high 14 (/ len 65536)) + (sha1-block block-high block-low)) + ;; need one more block. + (sha1-block block-high block-low) + (fillarray block-high 0) + (fillarray block-low 0) + ;; write bit length of string to last 4 bytes of the block. + (aset block-low 15 (* (% len 8192) 8)) + (setq len (/ len 8192)) + (aset block-high 15 (% len 65536)) + ;; XXX: It is not practical to compute SHA1 of + ;; such a huge message on emacs. + ;; (setq len (/ len 65536)) ; for 64bit emacs. + ;; (aset block-low 14 (% len 65536)) + ;; (aset block-high 14 (/ len 65536)) + (sha1-block block-high block-low)) + ;; make output string (in binary form). + (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)) + ;; do not leave a copy of input string. + (fillarray block-high nil) + (fillarray block-low nil)))) + +(defun sha1-string-internal (string) + (encode-hex-string (sha1-binary string))) + +(defun sha1-region-internal (beg end) + (sha1-string-internal (buffer-substring-no-properties beg end))) + +;;; +;;; application interface. +;;; + +(defun sha1-region (beg end) + (if (and sha1-maximum-internal-length + (> (abs (- end beg)) sha1-maximum-internal-length)) + (sha1-region-external beg end) + (sha1-region-internal beg end))) + +(defun sha1-string (string) + (if (and sha1-maximum-internal-length + (> (length string) sha1-maximum-internal-length)) + (sha1-string-external string) + (sha1-string-internal string))) + +(defun sha1 (object &optional beg end) + "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 (stringp object) + (sha1-string object) + (save-excursion + (set-buffer object) + (sha1-region (or beg (point-min)) (or end (point-max)))))) + +(provide 'sha1-el) + +;;; sha1-el.el ends here diff --git a/sha1.el b/sha1.el index 9818d02..c86eb55 100644 --- a/sha1.el +++ b/sha1.el @@ -38,25 +38,22 @@ ;;; Code: +(defvar sha1-dl-module + (if (and (fboundp 'sha1-string) + (subrp (symbol-function 'sha1-string))) + nil + (if (fboundp 'dynamic-link) + (let ((path (expand-file-name "sha1.so" exec-directory))) + (and (file-exists-p path) + path))))) + (cond - ((and (fboundp 'dynamic-link) - (file-exists-p (expand-file-name "sha1.so" exec-directory))) + (sha1-dl-module ;; Emacs with DL patch. - (require 'sha1 "sha1-dl")) + (require 'sha1-dl)) (t - (require 'sha1 "sha1-el") - (defun sha1 (object &optional beg end) - "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 (stringp object) - (sha1-encode object) - (save-excursion - (set-buffer object) - (sha1-encode - (buffer-substring-no-properties - (or beg (point-min)) (or end (point-max))))))) - )) - -;;; sha1.el ends here. + (require 'sha1-el))) + +(provide 'sha1) + +;;; sha1.el ends here