gnus-html-wash-images: Fix spec computing to include start/end
[gnus] / lisp / sha1.el
1 ;;; sha1.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
2
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Keywords: SHA1, FIPS 180-1
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This program is implemented from the definition of SHA-1 in FIPS PUB
27 ;; 180-1 (Federal Information Processing Standards Publication 180-1),
28 ;; "Announcing the Standard for SECURE HASH STANDARD".
29 ;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
30 ;; (EXCEPTION; two optimizations taken from GnuPG/cipher/sha1.c)
31 ;;
32 ;; Test cases from FIPS PUB 180-1.
33 ;;
34 ;; (sha1 "abc")
35 ;; => a9993e364706816aba3e25717850c26c9cd0d89d
36 ;;
37 ;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
38 ;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
39 ;;
40 ;; (sha1 (make-string 1000000 ?a))
41 ;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
42 ;;
43 ;; BUGS:
44 ;;  * It is assumed that length of input string is less than 2^29 bytes.
45 ;;  * It is caller's responsibility to make string (or region) unibyte.
46 ;;
47 ;; TODO:
48 ;;  * Rewrite from scratch!
49 ;;    This version is much faster than Keiichi Suzuki's another sha1.el,
50 ;;    but it is too dirty.
51
52 ;;; Code:
53
54 (require 'hex-util)
55
56 ;;;
57 ;;; external SHA1 function.
58 ;;;
59
60 (defgroup sha1 nil
61   "Elisp interface for SHA1 hash computation."
62   :version "22.1"
63   :group 'extensions)
64
65 (defcustom sha1-maximum-internal-length 500
66   "Maximum length of message to use Lisp version of SHA1 function.
67 If message is longer than this, `sha1-program' is used instead.
68
69 If this variable is set to 0, use external program only.
70 If this variable is set to nil, use internal function only."
71   :type 'integer
72   :group 'sha1)
73
74 (defcustom sha1-program '("sha1sum")
75   "Name of program to compute SHA1.
76 It must be a string \(program name\) or list of strings \(name and its args\)."
77   :type '(repeat string)
78   :group 'sha1)
79
80 (defcustom sha1-use-external (condition-case ()
81                                  (executable-find (car sha1-program))
82                                (error))
83   "Use external SHA1 program.
84 If this variable is set to nil, use internal function only."
85   :type 'boolean
86   :group 'sha1)
87
88 (defun sha1-string-external (string &optional binary)
89   (let ((default-directory "/") ;; in case otherwise non-existent
90         (process-connection-type nil) ;; pipe
91         prog args digest)
92     (if (consp sha1-program)
93         (setq prog (car sha1