annotate lisp/sha1.el @ 87880:d9edfa21892c

*** empty log message ***
author Thien-Thi Nguyen <ttn@gnuvola.org>
date Mon, 21 Jan 2008 14:03:09 +0000
parents 107ccd98fa12
children e9bef8bf2940
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86919
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
1 ;;; sha1.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
2
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004,
87649
107ccd98fa12 Merge from emacs--rel--22
Miles Bader <miles@gnu.org>
parents: 86919
diff changeset
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
86919
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
5
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
6 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
7 ;; Keywords: SHA1, FIPS 180-1
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
8
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
10
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 3, or (at your option)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
14 ;; any later version.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
15
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24 ;; Boston, MA 02110-1301, USA.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26 ;;; Commentary:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 ;; This program is implemented from the definition of SHA-1 in FIPS PUB
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
29 ;; 180-1 (Federal Information Processing Standards Publication 180-1),
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
30 ;; "Announcing the Standard for SECURE HASH STANDARD".
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31 ;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 ;; (EXCEPTION; two optimizations taken from GnuPG/cipher/sha1.c)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34 ;; Test cases from FIPS PUB 180-1.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
36 ;; (sha1 "abc")
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
37 ;; => a9993e364706816aba3e25717850c26c9cd0d89d
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
38 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
39 ;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
40 ;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 ;; (sha1 (make-string 1000000 ?a))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 ;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 ;; BUGS:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
46 ;; * It is assumed that length of input string is less than 2^29 bytes.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47 ;; * It is caller's responsibility to make string (or region) unibyte.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 ;; TODO:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
50 ;; * Rewrite from scratch!
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 ;; This version is much faster than Keiichi Suzuki's another sha1.el,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52 ;; but it is too dirty.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 ;;; Code:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56 (require 'hex-util)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
57
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59 ;;; external SHA1 function.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
61
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 (defgroup sha1 nil
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
63 "Elisp interface for SHA1 hash computation."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64 :version "22.1"
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 :group 'extensions)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 (defcustom sha1-maximum-internal-length 500
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 "*Maximum length of message to use Lisp version of SHA1 function.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 If message is longer than this, `sha1-program' is used instead.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71 If this variable is set to 0, use external program only.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 If this variable is set to nil, use internal function only."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73 :type 'integer
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 :group 'sha1)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
76 (defcustom sha1-program '("sha1sum")
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
77 "*Name of program to compute SHA1.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
78 It must be a string \(program name\) or list of strings \(name and its args\)."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
79 :type '(repeat string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
80 :group 'sha1)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
81
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
82 (defcustom sha1-use-external (condition-case ()
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
83 (executable-find (car sha1-program))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
84 (error))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
85 "*Use external SHA1 program.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
86 If this variable is set to nil, use internal function only."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
87 :type 'boolean
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
88 :group 'sha1)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
89
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
90 (defun sha1-string-external (string &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
91 (let (prog args digest default-enable-multibyte-characters)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
92 (if (consp sha1-program)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
93 (setq prog (car sha1-program)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
94 args (cdr sha1-program))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
95 (setq prog sha1-program
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
96 args nil))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
97 (with-temp-buffer
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
98 (insert string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
99 (apply (function call-process-region)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
100 (point-min)(point-max)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
101 prog t t nil args)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
102 ;; SHA1 is 40 bytes long in hexadecimal form.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
103 (setq digest (buffer-substring (point-min)(+ (point-min) 40))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
104 (if binary
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
105 (decode-hex-string digest)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
106 digest)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
107
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
108 (defun sha1-region-external (beg end &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
109 (sha1-string-external (buffer-substring-no-properties beg end) binary))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
110
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
111 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
112 ;;; internal SHA1 function.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
113 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
114
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
115 (eval-when-compile
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
116 ;; optional second arg of string-to-number is new in v20.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
117 (defconst sha1-K0-high 23170) ; (string-to-number "5A82" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
118 (defconst sha1-K0-low 31129) ; (string-to-number "7999" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
119 (defconst sha1-K1-high 28377) ; (string-to-number "6ED9" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
120 (defconst sha1-K1-low 60321) ; (string-to-number "EBA1" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
121 (defconst sha1-K2-high 36635) ; (string-to-number "8F1B" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
122 (defconst sha1-K2-low 48348) ; (string-to-number "BCDC" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
123 (defconst sha1-K3-high 51810) ; (string-to-number "CA62" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
124 (defconst sha1-K3-low 49622) ; (string-to-number "C1D6" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
125
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
126 ;; original definition of sha1-F0.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
127 ;; (defmacro sha1-F0 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
128 ;; (` (logior (logand (, B) (, C))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
129 ;; (logand (lognot (, B)) (, D)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
130 ;; a little optimization from GnuPG/cipher/sha1.c.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
131 (defmacro sha1-F0 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
132 `(logxor ,D (logand ,B (logxor ,C ,D))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
133 (defmacro sha1-F1 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
134 `(logxor ,B ,C ,D))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
135 ;; original definition of sha1-F2.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
136 ;; (defmacro sha1-F2 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
137 ;; (` (logior (logand (, B) (, C))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
138 ;; (logand (, B) (, D))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
139 ;; (logand (, C) (, D)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
140 ;; a little optimization from GnuPG/cipher/sha1.c.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
141 (defmacro sha1-F2 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
142 `(logior (logand ,B ,C)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
143 (logand ,D (logior ,B ,C))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
144 (defmacro sha1-F3 (B C D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
145 `(logxor ,B ,C ,D))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
146
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
147 (defmacro sha1-S1 (W-high W-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
148 `(let ((W-high ,W-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
149 (W-low ,W-low))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
150 (setq S1W-high (+ (% (* W-high 2) 65536)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
151 (/ W-low ,(/ 65536 2))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
152 (setq S1W-low (+ (/ W-high ,(/ 65536 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
153 (% (* W-low 2) 65536)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
154 (defmacro sha1-S5 (A-high A-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
155 `(progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
156 (setq S5A-high (+ (% (* ,A-high 32) 65536)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
157 (/ ,A-low ,(/ 65536 32))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
158 (setq S5A-low (+ (/ ,A-high ,(/ 65536 32))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
159 (% (* ,A-low 32) 65536)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
160 (defmacro sha1-S30 (B-high B-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
161 `(progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
162 (setq S30B-high (+ (/ ,B-high 4)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
163 (* (% ,B-low 4) ,(/ 65536 4))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
164 (setq S30B-low (+ (/ ,B-low 4)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
165 (* (% ,B-high 4) ,(/ 65536 4))))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
166
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
167 (defmacro sha1-OP (round)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
168 `(progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
169 (sha1-S5 sha1-A-high sha1-A-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
170 (sha1-S30 sha1-B-high sha1-B-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
171 (setq sha1-A-low (+ (,(intern (format "sha1-F%d" round))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
172 sha1-B-low sha1-C-low sha1-D-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
173 sha1-E-low
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
174 ,(symbol-value
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
175 (intern (format "sha1-K%d-low" round)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
176 (aref block-low idx)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
177 (progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
178 (setq sha1-E-low sha1-D-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
179 (setq sha1-D-low sha1-C-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
180 (setq sha1-C-low S30B-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
181 (setq sha1-B-low sha1-A-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
182 S5A-low)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
183 (setq carry (/ sha1-A-low 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
184 (setq sha1-A-low (% sha1-A-low 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
185 (setq sha1-A-high (% (+ (,(intern (format "sha1-F%d" round))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
186 sha1-B-high sha1-C-high sha1-D-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
187 sha1-E-high
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
188 ,(symbol-value
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
189 (intern (format "sha1-K%d-high" round)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
190 (aref block-high idx)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
191 (progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
192 (setq sha1-E-high sha1-D-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
193 (setq sha1-D-high sha1-C-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
194 (setq sha1-C-high S30B-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
195 (setq sha1-B-high sha1-A-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
196 S5A-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
197 carry)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
198 65536))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
199
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
200 (defmacro sha1-add-to-H (H X)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
201 `(progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
202 (setq ,(intern (format "sha1-%s-low" H))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
203 (+ ,(intern (format "sha1-%s-low" H))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
204 ,(intern (format "sha1-%s-low" X))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
205 (setq carry (/ ,(intern (format "sha1-%s-low" H)) 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
206 (setq ,(intern (format "sha1-%s-low" H))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
207 (% ,(intern (format "sha1-%s-low" H)) 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
208 (setq ,(intern (format "sha1-%s-high" H))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
209 (% (+ ,(intern (format "sha1-%s-high" H))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
210 ,(intern (format "sha1-%s-high" X))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
211 carry)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
212 65536))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
213 )
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
214
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
215 ;;; buffers (H0 H1 H2 H3 H4).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
216 (defvar sha1-H0-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
217 (defvar sha1-H0-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
218 (defvar sha1-H1-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
219 (defvar sha1-H1-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
220 (defvar sha1-H2-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
221 (defvar sha1-H2-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
222 (defvar sha1-H3-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
223 (defvar sha1-H3-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
224 (defvar sha1-H4-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
225 (defvar sha1-H4-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
226
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
227 (defun sha1-block (block-high block-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
228 (let (;; step (c) --- initialize buffers (A B C D E).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
229 (sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
230 (sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
231 (sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
232 (sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
233 (sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
234 (idx 16))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
235 ;; step (b).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
236 (let (;; temporary variables used in sha1-S1 macro.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
237 S1W-high S1W-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
238 (while (< idx 80)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
239 (sha1-S1 (logxor (aref block-high (- idx 3))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
240 (aref block-high (- idx 8))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
241 (aref block-high (- idx 14))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
242 (aref block-high (- idx 16)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
243 (logxor (aref block-low (- idx 3))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
244 (aref block-low (- idx 8))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
245 (aref block-low (- idx 14))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
246 (aref block-low (- idx 16))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
247 (aset block-high idx S1W-high)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
248 (aset block-low idx S1W-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
249 (setq idx (1+ idx))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
250 ;; step (d).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
251 (setq idx 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
252 (let (;; temporary variables used in sha1-OP macro.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
253 S5A-high S5A-low S30B-high S30B-low carry)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
254 (while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
255 (while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
256 (while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
257 (while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
258 ;; step (e).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
259 (let (;; temporary variables used in sha1-add-to-H macro.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
260 carry)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
261 (sha1-add-to-H H0 A)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
262 (sha1-add-to-H H1 B)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
263 (sha1-add-to-H H2 C)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
264 (sha1-add-to-H H3 D)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
265 (sha1-add-to-H H4 E))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
266
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
267 (defun sha1-binary (string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
268 "Return the SHA1 of STRING in binary form."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
269 (let (;; prepare buffers for a block. byte-length of block is 64.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
270 ;; input block is split into two vectors.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
271 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
272 ;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ...
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
273 ;; block-high: +-0-+ +-1-+ +-2-+ +-3-+
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
274 ;; block-low: +-0-+ +-1-+ +-2-+ +-3-+
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
275 ;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
276 ;; length of each vector is 80, and elements of each vector are
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
277 ;; 16bit integers. elements 0x10-0x4F of each vector are
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
278 ;; assigned later in `sha1-block'.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
279 (block-high (eval-when-compile (make-vector 80 nil)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
280 (block-low (eval-when-compile (make-vector 80 nil))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
281 (unwind-protect
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
282 (let* (;; byte-length of input string.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
283 (len (length string))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
284 (lim (* (/ len 64) 64))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
285 (rem (% len 4))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
286 (idx 0)(pos 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
287 ;; initialize buffers (H0 H1 H2 H3 H4).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
288 (setq sha1-H0-high 26437 ; (string-to-number "6745" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
289 sha1-H0-low 8961 ; (string-to-number "2301" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
290 sha1-H1-high 61389 ; (string-to-number "EFCD" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
291 sha1-H1-low 43913 ; (string-to-number "AB89" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
292 sha1-H2-high 39098 ; (string-to-number "98BA" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
293 sha1-H2-low 56574 ; (string-to-number "DCFE" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
294 sha1-H3-high 4146 ; (string-to-number "1032" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
295 sha1-H3-low 21622 ; (string-to-number "5476" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
296 sha1-H4-high 50130 ; (string-to-number "C3D2" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
297 sha1-H4-low 57840) ; (string-to-number "E1F0" 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
298 ;; loop for each 64 bytes block.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
299 (while (< pos lim)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
300 ;; step (a).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
301 (setq idx 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
302 (while (< idx 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
303 (aset block-high idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
304 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
305 (setq pos (+ pos 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
306 (aset block-low idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
307 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
308 (setq pos (+ pos 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
309 (setq idx (1+ idx)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
310 (sha1-block block-high block-low))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
311 ;; last block.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
312 (if (prog1
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
313 (< (- len lim) 56)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
314 (setq lim (- len rem))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
315 (setq idx 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
316 (while (< pos lim)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
317 (aset block-high idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
318 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
319 (setq pos (+ pos 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
320 (aset block-low idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
321 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
322 (setq pos (+ pos 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
323 (setq idx (1+ idx)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
324 ;; this is the last (at most) 32bit word.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
325 (cond
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
326 ((= rem 3)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
327 (aset block-high idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
328 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
329 (setq pos (+ pos 2))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
330 (aset block-low idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
331 128)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
332 ((= rem 2)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
333 (aset block-high idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
334 (aref string (1+ pos))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
335 (aset block-low idx 32768))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
336 ((= rem 1)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
337 (aset block-high idx (+ (* (aref string pos) 256)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
338 128))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
339 (aset block-low idx 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
340 (t ;; (= rem 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
341 (aset block-high idx 32768)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
342 (aset block-low idx 0)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
343 (setq idx (1+ idx))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
344 (while (< idx 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
345 (aset block-high idx 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
346 (aset block-low idx 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
347 (setq idx (1+ idx))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
348 ;; last block has enough room to write the length of string.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
349 (progn
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
350 ;; write bit length of string to last 4 bytes of the block.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
351 (aset block-low 15 (* (% len 8192) 8))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
352 (setq len (/ len 8192))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
353 (aset block-high 15 (% len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
354 ;; XXX: It is not practical to compute SHA1 of
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
355 ;; such a huge message on emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
356 ;; (setq len (/ len 65536)) ; for 64bit emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
357 ;; (aset block-low 14 (% len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
358 ;; (aset block-high 14 (/ len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
359 (sha1-block block-high block-low))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
360 ;; need one more block.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
361 (sha1-block block-high block-low)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
362 (fillarray block-high 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
363 (fillarray block-low 0)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
364 ;; write bit length of string to last 4 bytes of the block.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
365 (aset block-low 15 (* (% len 8192) 8))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
366 (setq len (/ len 8192))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
367 (aset block-high 15 (% len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
368 ;; XXX: It is not practical to compute SHA1 of
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
369 ;; such a huge message on emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
370 ;; (setq len (/ len 65536)) ; for 64bit emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
371 ;; (aset block-low 14 (% len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
372 ;; (aset block-high 14 (/ len 65536))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
373 (sha1-block block-high block-low))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
374 ;; make output string (in binary form).
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
375 (let ((result (make-string 20 0)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
376 (aset result 0 (/ sha1-H0-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
377 (aset result 1 (% sha1-H0-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
378 (aset result 2 (/ sha1-H0-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
379 (aset result 3 (% sha1-H0-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
380 (aset result 4 (/ sha1-H1-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
381 (aset result 5 (% sha1-H1-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
382 (aset result 6 (/ sha1-H1-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
383 (aset result 7 (% sha1-H1-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
384 (aset result 8 (/ sha1-H2-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
385 (aset result 9 (% sha1-H2-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
386 (aset result 10 (/ sha1-H2-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
387 (aset result 11 (% sha1-H2-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
388 (aset result 12 (/ sha1-H3-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
389 (aset result 13 (% sha1-H3-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
390 (aset result 14 (/ sha1-H3-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
391 (aset result 15 (% sha1-H3-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
392 (aset result 16 (/ sha1-H4-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
393 (aset result 17 (% sha1-H4-high 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
394 (aset result 18 (/ sha1-H4-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
395 (aset result 19 (% sha1-H4-low 256))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
396 result))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
397 ;; do not leave a copy of input string.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
398 (fillarray block-high nil)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
399 (fillarray block-low nil))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
400
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
401 (defun sha1-string-internal (string &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
402 (if binary
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
403 (sha1-binary string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
404 (encode-hex-string (sha1-binary string))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
405
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
406 (defun sha1-region-internal (beg end &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
407 (sha1-string-internal (buffer-substring-no-properties beg end) binary))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
408
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
409 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
410 ;;; application interface.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
411 ;;;
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
412
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
413 (defun sha1-region (beg end &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
414 (if (and sha1-use-external
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
415 sha1-maximum-internal-length
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
416 (> (abs (- end beg)) sha1-maximum-internal-length))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
417 (sha1-region-external beg end binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
418 (sha1-region-internal beg end binary)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
419
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
420 (defun sha1-string (string &optional binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
421 (if (and sha1-use-external
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
422 sha1-maximum-internal-length
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
423 (> (length string) sha1-maximum-internal-length))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
424 (sha1-string-external string binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
425 (sha1-string-internal string binary)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
426
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
427 ;;;###autoload
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
428 (defun sha1 (object &optional beg end binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
429 "Return the SHA1 (Secure Hash Algorithm) of an object.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
430 OBJECT is either a string or a buffer.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
431 Optional arguments BEG and END denote buffer positions for computing the
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
432 hash of a portion of OBJECT.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
433 If BINARY is non-nil, return a string in binary form."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
434 (if (stringp object)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
435 (sha1-string object binary)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
436 (with-current-buffer object
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
437 (sha1-region (or beg (point-min)) (or end (point-max)) binary))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
438
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
439 (provide 'sha1)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
440
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
441 ;; arch-tag: c0f9abd0-ffc1-4557-aac6-ece7f2d4c901
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
442 ;;; sha1.el ends here