aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeo Liu2011-05-24 16:22:58 +0800
committerLeo Liu2011-05-24 16:22:58 +0800
commite1b90ef6eca2e32b99fff7ecf14bd1f074046da8 (patch)
tree8c55d1013121e1905517168deaa0e6dfd6153782
parent4ba4c54add7f291e655fb0a5555f7049a9ed17e9 (diff)
downloademacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.tar.gz
emacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.zip
Implement primitive `sha1' and remove sha1.el
-rw-r--r--lisp/ChangeLog9
-rw-r--r--lisp/bindings.el3
-rw-r--r--lisp/sha1.el441
-rw-r--r--lisp/vc/vc-bzr.el12
-rw-r--r--src/ChangeLog9
-rw-r--r--src/deps.mk3
-rw-r--r--src/fns.c129
-rw-r--r--src/makefile.w32-in1
8 files changed, 124 insertions, 483 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index e9e7faa93d4..bf7977ced08 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,12 @@
12011-05-24 Leo Liu <sdl.web@gmail.com>
2
3 * vc/vc-bzr.el (vc-bzr-sha1-program): Rename from sha1-program.
4 (vc-bzr-sha1): Adapt.
5
6 * sha1.el: Remove. Function `sha1' is now builtin.
7
8 * bindings.el: Provide sha1 feature.
9
12011-05-24 Kenichi Handa <handa@m17n.org> 102011-05-24 Kenichi Handa <handa@m17n.org>
2 11
3 * mail/sendmail.el: Require `rfc2047'. 12 * mail/sendmail.el: Require `rfc2047'.
diff --git a/lisp/bindings.el b/lisp/bindings.el
index 8c48bdc5d59..63c83ada9ba 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -646,9 +646,10 @@ is okay. See `mode-line-format'.")
646 646
647(make-variable-buffer-local 'indent-tabs-mode) 647(make-variable-buffer-local 'indent-tabs-mode)
648 648
649;; We have base64 and md5 functions built in now. 649;; We have base64, md5 and sha1 functions built in now.
650(provide 'base64) 650(provide 'base64)
651(provide 'md5) 651(provide 'md5)
652(provide 'sha1)
652(provide 'overlay '(display syntax-table field)) 653(provide 'overlay '(display syntax-table field))
653(provide 'text-properties '(display syntax-table field point-entered)) 654(provide 'text-properties '(display syntax-table field point-entered))
654 655
diff --git a/lisp/sha1.el b/lisp/sha1.el
deleted file mode 100644
index 3f2e8f2a69b..00000000000
--- a/lisp/sha1.el
+++ /dev/null
@@ -1,441 +0,0 @@
1;;; sha1.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
2
3;; Copyright (C) 1999, 2001-2011 Free Software Foundation, Inc.
4
5;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6;; Keywords: SHA1, FIPS 180-1
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;; This program is implemented from the definition of SHA-1 in FIPS PUB
26;; 180-1 (Federal Information Processing Standards Publication 180-1),
27;; "Announcing the Standard for SECURE HASH STANDARD".
28;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
29;; (EXCEPTION; two optimizations taken from GnuPG/cipher/sha1.c)
30;;
31;; Test cases from FIPS PUB 180-1.
32;;
33;; (sha1 "abc")
34;; => a9993e364706816aba3e25717850c26c9cd0d89d
35;;
36;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
37;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
38;;
39;; (sha1 (make-string 1000000 ?a))
40;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
41;;
42;; BUGS:
43;; * It is assumed that length of input string is less than 2^29 bytes.
44;; * It is caller's responsibility to make string (or region) unibyte.
45;;
46;; TODO:
47;; * Rewrite from scratch!
48;; This version is much faster than Keiichi Suzuki's another sha1.el,
49;; but it is too dirty.
50
51;;; Code:
52
53(require 'hex-util)
54
55;;;
56;;; external SHA1 function.
57;;;
58
59(defgroup sha1 nil
60 "Elisp interface for SHA1 hash computation."
61 :version "22.1"
62 :group 'extensions)
63
64(defcustom sha1-maximum-internal-length 500
65 "Maximum length of message to use Lisp version of SHA1 function.
66If message is longer than this, `sha1-program' is used instead.
67
68If this variable is set to 0, use external program only.
69If this variable is set to nil, use internal function only."
70 :type 'integer
71 :group 'sha1)
72
73(defcustom sha1-program '("sha1sum")
74 "Name of program to compute SHA1.
75It must be a string \(program name\) or list of strings \(name and its args\)."
76 :type '(repeat string)
77 :group 'sha1)
78
79(defcustom sha1-use-external (condition-case ()
80 (executable-find (car sha1-program))
81 (error))
82 "Use external SHA1 program.
83If this variable is set to nil, use internal function only."
84 :type 'boolean
85 :group 'sha1)
86
87(defun sha1-string-external (string &optional binary)
88 (let ((default-directory "/") ;; in case otherwise non-existent
89 (process-connection-type nil) ;; pipe
90 prog args digest)
91 (if (consp sha1-program)
92 (setq prog (car sha1-program)
93 args (cdr sha1-program))
94 (setq prog sha1-program
95 args nil))
96 (with-temp-buffer
97 (unless (featurep 'xemacs) (set-buffer-multibyte nil))
98 (insert string)
99 (apply (function call-process-region)
100 (point-min) (point-max)
101 prog t t nil args)
102 ;; SHA1 is 40 bytes long in hexadecimal form.
103 (setq digest (buffer-substring (point-min)(+ (point-min) 40))))
104 (if binary
105 (decode-hex-string digest)
106 digest)))
107
108(defun sha1-region-external (beg end &optional binary)
109 (sha1-string-external (buffer-substring-no-properties beg end) binary))
110
111;;;
112;;; internal SHA1 function.
113;;;
114
115(eval-when-compile
116 ;; optional second arg of string-to-number is new in v20.
117 (defconst sha1-K0-high 23170) ; (string-to-number "5A82" 16)
118 (defconst sha1-K0-low 31129) ; (string-to-number "7999" 16)
119 (defconst sha1-K1-high 28377) ; (string-to-number "6ED9" 16)
120 (defconst sha1-K1-low 60321) ; (string-to-number "EBA1" 16)
121 (defconst sha1-K2-high 36635) ; (string-to-number "8F1B" 16)
122 (defconst sha1-K2-low 48348) ; (string-to-number "BCDC" 16)
123 (defconst sha1-K3-high 51810) ; (string-to-number "CA62" 16)
124 (defconst sha1-K3-low 49622) ; (string-to-number "C1D6" 16)
125
126 ;; original definition of sha1-F0.
127 ;; (defmacro sha1-F0 (B C D)
128 ;; (` (logior (logand (, B) (, C))
129 ;; (logand (lognot (, B)) (, D)))))
130 ;; a little optimization from GnuPG/cipher/sha1.c.
131 (defmacro sha1-F0 (B C D)
132 `(logxor ,D (logand ,B (logxor ,C ,D))))
133 (defmacro sha1-F1 (B C D)
134 `(logxor ,B ,C ,D))
135 ;; original definition of sha1-F2.
136 ;; (defmacro sha1-F2 (B C D)
137 ;; (` (logior (logand (, B) (, C))
138 ;; (logand (, B) (, D))
139 ;; (logand (, C) (, D)))))
140 ;; a little optimization from GnuPG/cipher/sha1.c.
141 (defmacro sha1-F2 (B C D)
142 `(logior (logand ,B ,C)
143 (logand ,D (logior ,B ,C))))
144 (defmacro sha1-F3 (B C D)
145 `(logxor ,B ,C ,D))
146
147 (defmacro sha1-S1 (W-high W-low)
148 `(let ((W-high ,W-high)
149 (W-low ,W-low))
150 (setq S1W-high (+ (% (* W-high 2) 65536)
151 (/ W-low ,(/ 65536 2))))
152 (setq S1W-low (+ (/ W-high ,(/ 65536 2))
153 (% (* W-low 2) 65536)))))
154 (defmacro sha1-S5 (A-high A-low)
155 `(progn
156 (setq S5A-high (+ (% (* ,A-high 32) 65536)
157 (/ ,A-low ,(/ 65536 32))))
158 (setq S5A-low (+ (/ ,A-high ,(/ 65536 32))
159 (% (* ,A-low 32) 65536)))))
160 (defmacro sha1-S30 (B-high B-low)
161 `(progn
162 (setq S30B-high (+ (/ ,B-high 4)
163 (* (% ,B-low 4) ,(/ 65536 4))))
164 (setq S30B-low (+ (/ ,B-low 4)
165 (* (% ,B-high 4) ,(/ 65536 4))))))
166
167 (defmacro sha1-OP (round)
168 `(progn
169 (sha1-S5 sha1-A-high sha1-A-low)
170 (sha1-S30 sha1-B-high sha1-B-low)
171 (setq sha1-A-low (+ (,(intern (format "sha1-F%d" round))
172 sha1-B-low sha1-C-low sha1-D-low)
173 sha1-E-low
174 ,(symbol-value
175 (intern (format "sha1-K%d-low" round)))
176 (aref block-low idx)
177 (progn
178 (setq sha1-E-low sha1-D-low)
179 (setq sha1-D-low sha1-C-low)
180 (setq sha1-C-low S30B-low)
181 (setq sha1-B-low sha1-A-low)
182 S5A-low)))
183 (setq carry (/ sha1-A-low 65536))
184 (setq sha1-A-low (% sha1-A-low 65536))
185 (setq sha1-A-high (% (+ (,(intern (format "sha1-F%d" round))
186 sha1-B-high sha1-C-high sha1-D-high)
187 sha1-E-high
188 ,(symbol-value
189 (intern (format "sha1-K%d-high" round)))
190 (aref block-high idx)
191 (progn
192 (setq sha1-E-high sha1-D-high)
193 (setq sha1-D-high sha1-C-high)
194 (setq sha1-C-high S30B-high)
195 (setq sha1-B-high sha1-A-high)
196 S5A-high)
197 carry)
198 65536))))
199
200 (defmacro sha1-add-to-H (H X)
201 `(progn
202 (setq ,(intern (format "sha1-%s-low" H))
203 (+ ,(intern (format "sha1-%s-low" H))
204 ,(intern (format "sha1-%s-low" X))))
205 (setq carry (/ ,(intern (format "sha1-%s-low" H)) 65536))
206 (setq ,(intern (format "sha1-%s-low" H))
207 (% ,(intern (format "sha1-%s-low" H)) 65536))
208 (setq ,(intern (format "sha1-%s-high" H))
209 (% (+ ,(intern (format "sha1-%s-high" H))
210 ,(intern (format "sha1-%s-high" X))
211 carry)
212 65536))))
213 )
214
215;;; buffers (H0 H1 H2 H3 H4).
216(defvar sha1-H0-high)
217(defvar sha1-H0-low)
218(defvar sha1-H1-high)
219(defvar sha1-H1-low)
220(defvar sha1-H2-high)
221(defvar sha1-H2-low)
222(defvar sha1-H3-high)
223(defvar sha1-H3-low)
224(defvar sha1-H4-high)
225(defvar sha1-H4-low)
226
227(defun sha1-block (block-high block-low)
228 (let (;; step (c) --- initialize buffers (A B C D E).
229 (sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
230 (sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
231 (sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
232 (sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
233 (sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
234 (idx 16))
235 ;; step (b).
236 (let (;; temporary variables used in sha1-S1 macro.
237 S1W-high S1W-low)
238 (while (< idx 80)
239 (sha1-S1 (logxor (aref block-high (- idx 3))
240 (aref block-high (- idx 8))
241 (aref block-high (- idx 14))
242 (aref block-high (- idx 16)))
243 (logxor (aref block-low (- idx 3))
244 (aref block-low (- idx 8))
245 (aref block-low (- idx 14))
246 (aref block-low (- idx 16))))
247 (aset block-high idx S1W-high)
248 (aset block-low idx S1W-low)
249 (setq idx (1+ idx))))
250 ;; step (d).
251 (setq idx 0)
252 (let (;; temporary variables used in sha1-OP macro.
253 S5A-high S5A-low S30B-high S30B-low carry)
254 (while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
255 (while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
256 (while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
257 (while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
258 ;; step (e).
259 (let (;; temporary variables used in sha1-add-to-H macro.
260 carry)
261 (sha1-add-to-H H0 A)
262 (sha1-add-to-H H1 B)
263 (sha1-add-to-H H2 C)
264 (sha1-add-to-H H3 D)
265 (sha1-add-to-H H4 E))))
266
267(defun sha1-binary (string)
268 "Return the SHA1 of STRING in binary form."
269 (let (;; prepare buffers for a block. byte-length of block is 64.
270 ;; input block is split into two vectors.
271 ;;
272 ;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ...
273 ;; block-high: +-0-+ +-1-+ +-2-+ +-3-+
274 ;; block-low: +-0-+ +-1-+ +-2-+ +-3-+
275 ;;
276 ;; length of each vector is 80, and elements of each vector are
277 ;; 16bit integers. elements 0x10-0x4F of each vector are
278 ;; assigned later in `sha1-block'.
279 (block-high (eval-when-compile (make-vector 80 nil)))
280 (block-low (eval-when-compile (make-vector 80 nil))))
281 (unwind-protect
282 (let* (;; byte-length of input string.
283 (len (length string))
284 (lim (* (/ len 64) 64))
285 (rem (% len 4))
286 (idx 0)(pos 0))
287 ;; initialize buffers (H0 H1 H2 H3 H4).
288 (setq sha1-H0-high 26437 ; (string-to-number "6745" 16)
289 sha1-H0-low 8961 ; (string-to-number "2301" 16)
290 sha1-H1-high 61389 ; (string-to-number "EFCD" 16)
291 sha1-H1-low 43913 ; (string-to-number "AB89" 16)
292 sha1-H2-high 39098 ; (string-to-number "98BA" 16)
293 sha1-H2-low 56574 ; (string-to-number "DCFE" 16)
294 sha1-H3-high 4146 ; (string-to-number "1032" 16)
295 sha1-H3-low 21622 ; (string-to-number "5476" 16)
296 sha1-H4-high 50130 ; (string-to-number "C3D2" 16)
297 sha1-H4-low 57840) ; (string-to-number "E1F0" 16)
298 ;; loop for each 64 bytes block.
299 (while (< pos lim)
300 ;; step (a).
301 (setq idx 0)
302 (while (< idx 16)
303 (aset block-high idx (+ (* (aref string pos) 256)
304 (aref string (1+ pos))))
305 (setq pos (+ pos 2))
306 (aset block-low idx (+ (* (aref string pos) 256)
307 (aref string (1+ pos))))
308 (setq pos (+ pos 2))
309 (setq idx (1+ idx)))
310 (sha1-block block-high block-low))
311 ;; last block.
312 (if (prog1
313 (< (- len lim) 56)
314 (setq lim (- len rem))
315 (setq idx 0)
316 (while (< pos lim)
317 (aset block-high idx (+ (* (aref string pos) 256)
318 (aref string (1+ pos))))
319 (setq pos (+ pos 2))
320 (aset block-low idx (+ (* (aref string pos) 256)
321 (aref string (1+ pos))))
322 (setq pos (+ pos 2))
323 (setq idx (1+ idx)))
324 ;; this is the last (at most) 32bit word.
325 (cond
326 ((= rem 3)
327 (aset block-high idx (+ (* (aref string pos) 256)
328 (aref string (1+ pos))))
329 (setq pos (+ pos 2))
330 (aset block-low idx (+ (* (aref string pos) 256)
331 128)))
332 ((= rem 2)
333 (aset block-high idx (+ (* (aref string pos) 256)
334 (aref string (1+ pos))))
335 (aset block-low idx 32768))
336 ((= rem 1)
337 (aset block-high idx (+ (* (aref string pos) 256)
338 128))
339 (aset block-low idx 0))
340 (t ;; (= rem 0)
341 (aset block-high idx 32768)
342 (aset block-low idx 0)))
343 (setq idx (1+ idx))
344 (while (< idx 16)
345 (aset block-high idx 0)
346 (aset block-low idx 0)
347 (setq idx (1+ idx))))
348 ;; last block has enough room to write the length of string.
349 (progn
350 ;; write bit length of string to last 4 bytes of the block.
351 (aset block-low 15 (* (% len 8192) 8))
352 (setq len (/ len 8192))
353 (aset block-high 15 (% len 65536))
354 ;; XXX: It is not practical to compute SHA1 of
355 ;; such a huge message on emacs.
356 ;; (setq len (/ len 65536)) ; for 64bit emacs.
357 ;; (aset block-low 14 (% len 65536))
358 ;; (aset block-high 14 (/ len 65536))
359 (sha1-block block-high block-low))
360 ;; need one more block.
361 (sha1-block block-high block-low)
362 (fillarray block-high 0)
363 (fillarray block-low 0)
364 ;; write bit length of string to last 4 bytes of the block.
365 (aset block-low 15 (* (% len 8192) 8))
366 (setq len (/ len 8192))
367 (aset block-high 15 (% len 65536))
368 ;; XXX: It is not practical to compute SHA1 of
369 ;; such a huge message on emacs.
370 ;; (setq len (/ len 65536)) ; for 64bit emacs.
371 ;; (aset block-low 14 (% len 65536))
372 ;; (aset block-high 14 (/ len 65536))
373 (sha1-block block-high block-low))
374 ;; make output string (in binary form).
375 (let ((result (make-string 20 0)))
376 (aset result 0 (/ sha1-H0-high 256))
377 (aset result 1 (% sha1-H0-high 256))
378 (aset result 2 (/ sha1-H0-low 256))
379 (aset result 3 (% sha1-H0-low 256))
380 (aset result 4 (/ sha1-H1-high 256))
381 (aset result 5 (% sha1-H1-high 256))
382 (aset result 6 (/ sha1-H1-low 256))
383 (aset result 7 (% sha1-H1-low 256))
384 (aset result 8 (/ sha1-H2-high 256))
385 (aset result 9 (% sha1-H2-high 256))
386 (aset result 10 (/ sha1-H2-low 256))
387 (aset result 11 (% sha1-H2-low 256))
388 (aset result 12 (/ sha1-H3-high 256))
389 (aset result 13 (% sha1-H3-high 256))
390 (aset result 14 (/ sha1-H3-low 256))
391 (aset result 15 (% sha1-H3-low 256))
392 (aset result 16 (/ sha1-H4-high 256))
393 (aset result 17 (% sha1-H4-high 256))
394 (aset result 18 (/ sha1-H4-low 256))
395 (aset result 19 (% sha1-H4-low 256))
396 result))
397 ;; do not leave a copy of input string.
398 (fillarray block-high nil)
399 (fillarray block-low nil))))
400
401(defun sha1-string-internal (string &optional binary)
402 (if binary
403 (sha1-binary string)
404 (encode-hex-string (sha1-binary string))))
405
406(defun sha1-region-internal (beg end &optional binary)
407 (sha1-string-internal (buffer-substring-no-properties beg end) binary))
408
409;;;
410;;; application interface.
411;;;
412
413(defun sha1-region (beg end &optional binary)
414 (if (and sha1-use-external
415 sha1-maximum-internal-length
416 (> (abs (- end beg)) sha1-maximum-internal-length))
417 (sha1-region-external beg end binary)
418 (sha1-region-internal beg end binary)))
419
420(defun sha1-string (string &optional binary)
421 (if (and sha1-use-external
422 sha1-maximum-internal-length
423 (> (length string) sha1-maximum-internal-length))
424 (sha1-string-external string binary)
425 (sha1-string-internal string binary)))
426
427;;;###autoload
428(defun sha1 (object &optional beg end binary)
429 "Return the SHA1 (Secure Hash Algorithm) of an object.
430OBJECT is either a string or a buffer.
431Optional arguments BEG and END denote buffer positions for computing the
432hash of a portion of OBJECT.
433If BINARY is non-nil, return a string in binary form."
434 (if (stringp object)
435 (sha1-string object binary)
436 (with-current-buffer object
437 (sha1-region (or beg (point-min)) (or end (point-max)) binary))))
438
439(provide 'sha1)
440
441;;; sha1.el ends here
diff --git a/lisp/vc/vc-bzr.el b/lisp/vc/vc-bzr.el
index 21cb86a9840..fa59b7ef19c 100644
--- a/lisp/vc/vc-bzr.el
+++ b/lisp/vc/vc-bzr.el
@@ -65,6 +65,14 @@
65 :group 'vc-bzr 65 :group 'vc-bzr
66 :type 'string) 66 :type 'string)
67 67
68(defcustom vc-bzr-sha1-program '("sha1sum")
69 "Name of program to compute SHA1.
70It must be a string \(program name\) or list of strings \(name and its args\)."
71 :type '(repeat string)
72 :group 'vc-bzr)
73
74(define-obsolete-variable-alias 'sha1-program 'vc-bzr-sha1-program "24.1")
75
68(defcustom vc-bzr-diff-switches nil 76(defcustom vc-bzr-diff-switches nil
69 "String or list of strings specifying switches for bzr diff under VC. 77 "String or list of strings specifying switches for bzr diff under VC.
70If nil, use the value of `vc-diff-switches'. If t, use no switches." 78If nil, use the value of `vc-diff-switches'. If t, use no switches."
@@ -156,12 +164,10 @@ in the repository root directory of FILE."
156 (push (cons (match-string 1) (match-string 2)) settings))) 164 (push (cons (match-string 1) (match-string 2)) settings)))
157 settings)) 165 settings))
158 166
159(require 'sha1) ;For sha1-program
160
161(defun vc-bzr-sha1 (file) 167(defun vc-bzr-sha1 (file)
162 (with-temp-buffer 168 (with-temp-buffer
163 (set-buffer-multibyte nil) 169 (set-buffer-multibyte nil)
164 (let ((prog sha1-program) 170 (let ((prog vc-bzr-sha1-program)
165 (args nil) 171 (args nil)
166 process-file-side-effects) 172 process-file-side-effects)
167 (when (consp prog) 173 (when (consp prog)
diff --git a/src/ChangeLog b/src/ChangeLog
index c5594b8555d..b3b561a9370 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,12 @@
12011-05-24 Leo Liu <sdl.web@gmail.com>
2
3 * deps.mk (fns.o):
4 * makefile.w32-in ($(BLD)/fns.$(O)): Include sha1.h.
5
6 * fns.c (crypto_hash_function, Fsha1): New function.
7 (Fmd5): Use crypto_hash_function.
8 (syms_of_fns): Add Ssha1.
9
12011-05-22 Paul Eggert <eggert@cs.ucla.edu> 102011-05-22 Paul Eggert <eggert@cs.ucla.edu>
2 11
3 * gnutls.c: Remove unused macros. 12 * gnutls.c: Remove unused macros.
diff --git a/src/deps.mk b/src/deps.mk
index 8d0e0e69589..6c677f0e6c6 100644
--- a/src/deps.mk
+++ b/src/deps.mk
@@ -284,7 +284,8 @@ eval.o: eval.c commands.h keyboard.h blockinput.h atimer.h systime.h frame.h \
284floatfns.o: floatfns.c syssignal.h lisp.h globals.h $(config_h) 284floatfns.o: floatfns.c syssignal.h lisp.h globals.h $(config_h)
285fns.o: fns.c commands.h lisp.h $(config_h) frame.h buffer.h character.h \ 285fns.o: fns.c commands.h lisp.h $(config_h) frame.h buffer.h character.h \
286 keyboard.h keymap.h window.h $(INTERVALS_H) coding.h ../lib/md5.h \ 286 keyboard.h keymap.h window.h $(INTERVALS_H) coding.h ../lib/md5.h \
287 blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h globals.h 287 ../lib/sha1.h blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h \
288 globals.h
288print.o: print.c process.h frame.h window.h buffer.h keyboard.h character.h \ 289print.o: print.c process.h frame.h window.h buffer.h keyboard.h character.h \
289 lisp.h globals.h $(config_h) termchar.h $(INTERVALS_H) msdos.h termhooks.h \ 290 lisp.h globals.h $(config_h) termchar.h $(INTERVALS_H) msdos.h termhooks.h \
290 blockinput.h atimer.h systime.h font.h charset.h coding.h ccl.h \ 291 blockinput.h atimer.h systime.h font.h charset.h coding.h ccl.h \
diff --git a/src/fns.c b/src/fns.c
index 16dc0fe0de2..9d73e48b928 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4514,42 +4514,17 @@ including negative integers. */)
4514 4514
4515 4515
4516/************************************************************************ 4516/************************************************************************
4517 MD5 4517 MD5 and SHA1
4518 ************************************************************************/ 4518 ************************************************************************/
4519 4519
4520#include "md5.h" 4520#include "md5.h"
4521#include "sha1.h"
4521 4522
4522DEFUN ("md5", Fmd5, Smd5, 1, 5, 0, 4523/* TYPE: 0 for md5, 1 for sha1. */
4523 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4524
4525A message digest is a cryptographic checksum of a document, and the
4526algorithm to calculate it is defined in RFC 1321.
4527
4528The two optional arguments START and END are character positions
4529specifying for which part of OBJECT the message digest should be
4530computed. If nil or omitted, the digest is computed for the whole
4531OBJECT.
4532 4524
4533The MD5 message digest is computed from the result of encoding the 4525Lisp_Object
4534text in a coding system, not directly from the internal Emacs form of 4526crypto_hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary)
4535the text. The optional fourth argument CODING-SYSTEM specifies which
4536coding system to encode the text with. It should be the same coding
4537system that you used or will use when actually writing the text into a
4538file.
4539
4540If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4541OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4542system would be chosen by default for writing this text into a file.
4543
4544If OBJECT is a string, the most preferred coding system (see the
4545command `prefer-coding-system') is used.
4546
4547If NOERROR is non-nil, silently assume the `raw-text' coding if the
4548guesswork fails. Normally, an error is signaled in such case. */)
4549 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4550{ 4527{
4551 unsigned char digest[16];
4552 char value[33];
4553 int i; 4528 int i;
4554 EMACS_INT size; 4529 EMACS_INT size;
4555 EMACS_INT size_byte = 0; 4530 EMACS_INT size_byte = 0;
@@ -4558,6 +4533,7 @@ guesswork fails. Normally, an error is signaled in such case. */)
4558 register EMACS_INT b, e; 4533 register EMACS_INT b, e;
4559 register struct buffer *bp; 4534 register struct buffer *bp;
4560 EMACS_INT temp; 4535 EMACS_INT temp;
4536 Lisp_Object res=Qnil;
4561 4537
4562 if (STRINGP (object)) 4538 if (STRINGP (object))
4563 { 4539 {
@@ -4728,15 +4704,93 @@ guesswork fails. Normally, an error is signaled in such case. */)
4728 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0); 4704 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
4729 } 4705 }
4730 4706
4731 md5_buffer (SSDATA (object) + start_byte, 4707 switch (type)
4732 SBYTES (object) - (size_byte - end_byte), 4708 {
4733 digest); 4709 case 0: /* MD5 */
4710 {
4711 unsigned char digest[16];
4712 md5_buffer (SSDATA (object) + start_byte,
4713 SBYTES (object) - (size_byte - end_byte),
4714 digest);
4734 4715
4735 for (i = 0; i < 16; i++) 4716 if (NILP(binary))
4736 sprintf (&value[2 * i], "%02x", digest[i]); 4717 {
4737 value[32] = '\0'; 4718 unsigned char value[33];
4719 for (i = 0; i < 16; i++)
4720 sprintf (&value[2 * i], "%02x", digest[i]);
4721 value[32] = '\0';
4722 res = make_string (value, 32);
4723 }
4724 else
4725 res = make_string (digest, 16);
4726 break;
4727 }
4738 4728
4739 return make_string (value, 32); 4729 case 1: /* SHA1 */
4730 {
4731 unsigned char digest[20];
4732 sha1_buffer (SDATA (object) + start_byte,
4733 SBYTES (object) - (size_byte - end_byte),
4734 digest);
4735 if (NILP(binary))
4736 {
4737 unsigned char value[41];
4738 for (i = 0; i < 20; i++)
4739 sprintf (&value[2 * i], "%02x", digest[i]);
4740 value[40] = '\0';
4741 res = make_string (value, 40);
4742 }
4743 else
4744 res = make_string (digest, 20);
4745 break;
4746 }
4747 }
4748
4749 return res;
4750}
4751
4752DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4753 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4754
4755A message digest is a cryptographic checksum of a document, and the
4756algorithm to calculate it is defined in RFC 1321.
4757
4758The two optional arguments START and END are character positions
4759specifying for which part of OBJECT the message digest should be
4760computed. If nil or omitted, the digest is computed for the whole
4761OBJECT.
4762
4763The MD5 message digest is computed from the result of encoding the
4764text in a coding system, not directly from the internal Emacs form of
4765the text. The optional fourth argument CODING-SYSTEM specifies which
4766coding system to encode the text with. It should be the same coding
4767system that you used or will use when actually writing the text into a
4768file.
4769
4770If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4771OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4772system would be chosen by default for writing this text into a file.
4773
4774If OBJECT is a string, the most preferred coding system (see the
4775command `prefer-coding-system') is used.
4776
4777If NOERROR is non-nil, silently assume the `raw-text' coding if the
4778guesswork fails. Normally, an error is signaled in such case. */)
4779 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4780{
4781 return crypto_hash_function (0, object, start, end, coding_system, noerror, Qnil);
4782}
4783
4784DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0,
4785 doc: /* Return the SHA-1 (Secure Hash Algorithm) of an OBJECT.
4786
4787OBJECT is either a string or a buffer. Optional arguments START and
4788END are character positions specifying which portion of OBJECT for
4789computing the hash. If BINARY is non-nil, return a string in binary
4790form. */)
4791 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
4792{
4793 return crypto_hash_function (1, object, start, end, Qnil, Qnil, binary);
4740} 4794}
4741 4795
4742 4796
@@ -4911,6 +4965,7 @@ this variable. */);
4911 defsubr (&Sbase64_encode_string); 4965 defsubr (&Sbase64_encode_string);
4912 defsubr (&Sbase64_decode_string); 4966 defsubr (&Sbase64_decode_string);
4913 defsubr (&Smd5); 4967 defsubr (&Smd5);
4968 defsubr (&Ssha1);
4914 defsubr (&Slocale_info); 4969 defsubr (&Slocale_info);
4915} 4970}
4916 4971
diff --git a/src/makefile.w32-in b/src/makefile.w32-in
index 71c4fa4c0ac..060b565b308 100644
--- a/src/makefile.w32-in
+++ b/src/makefile.w32-in
@@ -866,6 +866,7 @@ $(BLD)/fns.$(O) : \
866 $(EMACS_ROOT)/nt/inc/unistd.h \ 866 $(EMACS_ROOT)/nt/inc/unistd.h \
867 $(EMACS_ROOT)/nt/inc/sys/time.h \ 867 $(EMACS_ROOT)/nt/inc/sys/time.h \
868 $(EMACS_ROOT)/lib/md5.h \ 868 $(EMACS_ROOT)/lib/md5.h \
869 $(EMACS_ROOT)/lib/sha1.h \
869 $(LISP_H) \ 870 $(LISP_H) \
870 $(SRC)/atimer.h \ 871 $(SRC)/atimer.h \
871 $(SRC)/blockinput.h \ 872 $(SRC)/blockinput.h \