097a959add9ed04a0e9e26403087a1798af9cd0c
[elisp/flim.git] / hmac-md5.el
1 ;;; hmac-md5.el --- Compute HMAC-MD5.
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-MD5, MD5, KEYED-MD5, CRAM-MD5
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 ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
28 ;;
29 ;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
30 ;;  => "9294727a3638bb1c13f48ef8158bfc9d"
31 ;;
32 ;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
33 ;;  => "750c783e6ab0b503eaa86e310a5db738"
34 ;;
35 ;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
36 ;;  => "56be34521d144c88dbb8c733f0e8b3f6"
37 ;;
38 ;; (encode-hex-string
39 ;;  (hmac-md5
40 ;;   (make-string 50 ?\xcd)
41 ;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
42 ;;  => "697eaf0aca3a3aea3a75164746ffaa79"
43 ;;
44 ;; (encode-hex-string
45 ;;  (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
46 ;;  => "56461ef2342edc00f9bab995690efd4c"
47 ;;
48 ;; (encode-hex-string
49 ;;  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
50 ;;  => "56461ef2342edc00f9bab995"
51 ;;
52 ;; (encode-hex-string
53 ;;  (hmac-md5
54 ;;   "Test Using Larger Than Block-Size Key - Hash Key First"
55 ;;   (make-string 80 ?\xaa)))
56 ;;  => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
57 ;;
58 ;; (encode-hex-string
59 ;;  (hmac-md5
60 ;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
61 ;;   (make-string 80 ?\xaa)))
62 ;;  => "6f630fad67cda0ee1fb1f562db3aa53e"
63
64 ;;; Code:
65
66 (eval-when-compile (require 'hmac-def))
67 (require 'hex-util)                     ; (decode-hex-string STRING)
68 (require 'md5)                          ; expects (md5 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 'md5-binary)
74   ;; do nothing.
75   )
76  ((condition-case nil
77        ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
78        (md5 "" nil nil 'binary)         ; => "d41d8cd98f00b204e9800998ecf8427e"
79      (wrong-number-of-arguments nil))
80   (defun md5-binary (string)
81     "Return the MD5 of STRING in binary form."
82    (decode-hex-string (md5 string nil nil 'binary))))
83  (t
84   (defun md5-binary (string)
85     "Return the MD5 of STRING in binary form."
86    (decode-hex-string (md5 string)))))
87
88 (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
89 (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
90
91 (provide 'hmac-md5)
92
93 ;;; hmac-md5.el ends here