1 ;;; sha1.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Keywords: SHA1, FIPS 180-1
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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)
32 ;; Test cases from FIPS PUB 180-1.
35 ;; => a9993e364706816aba3e25717850c26c9cd0d89d
37 ;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
38 ;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
40 ;; (sha1 (make-string 1000000 ?a))
41 ;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
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.
48 ;; * Rewrite from scratch!
49 ;; This version is much faster than Keiichi Suzuki's another sha1.el,
50 ;; but it is too dirty.
57 ;;; external SHA1 function.
61 "Elisp interface for SHA1 hash computation."
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.
69 If this variable is set to 0, use external program only.
70 If this variable is set to nil, use internal function only."
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)
80 (defcustom sha1-use-external (condition-case ()
81 (executable-find (car sha1-program))
83 "Use external SHA1 program.
84 If this variable is set to nil, use internal function only."
88 (defun sha1-string-external (string &optional binary)
89 (let ((default-directory "/") ;; in case otherwise non-existent
90 (process-connection-type nil) ;; pipe
92 (if (consp sha1-program)