Update FSF's address in GPL notices.
[elisp/flim.git] / hmac-sha1.el
1 ;;; hmac-sha1.el --- Compute HMAC-SHA1.
2
3 ;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: HMAC, RFC 2104, HMAC-SHA1, SHA1, Cancel-Lock
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
28 ;;
29 ;; (encode-hex-string (hmac-sha1 "Hi There" (make-string 20 ?\x0b)))
30 ;;  => "b617318655057264e28bc0b6fb378c8ef146be00"
31 ;;
32 ;; (encode-hex-string (hmac-sha1 "what do ya want for nothing?" "Jefe"))
33 ;;  => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
34 ;;
35 ;; (encode-hex-string (hmac-sha1 (make-string 50 ?\xdd) (make-string 20 ?\xaa)))
36 ;;  => "125d7342b9ac11cd91a39af48aa17b4f63f175d3"
37 ;;
38 ;; (encode-hex-string
39 ;;  (hmac-sha1
40 ;;   (make-string 50 ?\xcd)
41 ;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
42 ;;  => "4c9007f4026250c6bc8414f9bf50c86c2d7235da"
43 ;;
44 ;; (encode-hex-string
45 ;;  (hmac-sha1 "Test With Truncation" (make-string 20 ?\x0c)))
46 ;;  => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"
47 ;;
48 ;; (encode-hex-string
49 ;;  (hmac-sha1-96 "Test With Truncation" (make-string 20 ?\x0c)))
50 ;;  => "4c1a03424b55e07fe7f27be1"
51 ;;
52 ;; (encode-hex-string
53 ;;  (hmac-sha1
54 ;;   "Test Using Larger Than Block-Size Key - Hash Key First"
55 ;;   (make-string 80 ?\xaa)))
56 ;;  => "aa4ae5e15272d00e95705637ce8a3b55ed402112"
57 ;;
58 ;; (encode-hex-string
59 ;;  (hmac-sha1
60 ;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
61 ;;   (make-string 80 ?\xaa)))
62 ;;  => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91"
63
64 ;;; Code:
65
66 (eval-when-compile (require 'hmac-def))
67 (require 'hex-util)                     ; (decode-hex-string STRING)
68 (require 'sha1)                         ; expects (sha1 STRING)
69
70 ;; To share *.elc files between Emacs w/ and w/o DL patch,
71 ;; this check must be done at load-time.
72 (cond
73  ((fboundp 'sha1-binary)
74   ;; do nothing.
75   )
76  (t
77   (defun sha1-binary (string)
78     "Return the SHA1 of STRING in binary form."
79     (decode-hex-string (sha1 string)))))
80
81 (define-hmac-function hmac-sha1 sha1-binary 64 20) ; => (hmac-sha1 TEXT KEY)
82 (define-hmac-function hmac-sha1-96 sha1-binary 64 20 96)
83
84 (provide 'hmac-sha1)
85
86 ;;; hmac-sha1.el ends here