9818d0241f37b8e08d95e4b618c828516b0191ed
[elisp/flim.git] / sha1.el
1 ;;; sha1.el --- SHA1 Secure Hash Algorithm.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: SHA1, FIPS 180-1
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.
14
15 ;; This program 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 this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Examples from FIPS PUB 180-1.
28 ;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
29 ;;
30 ;; (sha1 "abc")
31 ;; => a9993e364706816aba3e25717850c26c9cd0d89d
32 ;;
33 ;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
34 ;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
35 ;;
36 ;; (sha1 (make-string 1000000 ?a))
37 ;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
38
39 ;;; Code:
40
41 (cond
42  ((and (fboundp 'dynamic-link)
43        (file-exists-p (expand-file-name "sha1.so" exec-directory)))
44   ;; Emacs with DL patch.
45   (require 'sha1 "sha1-dl"))
46  (t
47   (require 'sha1 "sha1-el")
48   (defun sha1 (object &optional beg end)
49     "Return the SHA1 (Secure Hash Algorithm) of an object.
50 OBJECT is either a string or a buffer.
51 Optional arguments BEG and END denote buffer positions for computing the
52 hash of a portion of OBJECT."
53     (if (stringp object)
54         (sha1-encode object)
55       (save-excursion
56         (set-buffer object)
57         (sha1-encode
58          (buffer-substring-no-properties
59            (or beg (point-min)) (or end (point-max)))))))
60   ))
61
62 ;;; sha1.el ends here.