Update Copyright header.
[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 ;;      Kenichi OKADA <okada@opaopa.org>
7 ;; Maintainer: Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: HMAC, RFC 2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
9
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or
15 ;; (at your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
30 ;;
31 ;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
32 ;;  => "9294727a3638bb1c13f48ef8158bfc9d"
33 ;;
34 ;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
35 ;;  => "750c783e6ab0b503eaa86e310a5db738"
36 ;;
37 ;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
38 ;;  => "56be34521d144c88dbb8c733f0e8b3f6"
39 ;;
40 ;; (encode-hex-string
41 ;;  (hmac-md5
42 ;;   (make-string 50 ?\xcd)
43 ;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
44 ;;  => "697eaf0aca3a3aea3a75164746ffaa79"
45 ;;
46 ;; (encode-hex-string
47 ;;  (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
48 ;;  => "56461ef2342edc00f9bab995690efd4c"
49 ;;
50 ;; (encode-hex-string
51 ;;  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
52 ;;  => "56461ef2342edc00f9bab995"
53 ;;
54 ;; (encode-hex-string
55 ;;  (hmac-md5
56 ;;   "Test Using Larger Than Block-Size Key - Hash Key First"
57 ;;   (make-string 80 ?\xaa)))
58 ;;  => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
59 ;;
60 ;; (encode-hex-string
61 ;;  (hmac-md5
62 ;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
63 ;;   (make-string 80 ?\xaa)))
64 ;;  => "6f630fad67cda0ee1fb1f562db3aa53e"
65
66 ;;; Code:
67
68 (eval-when-compile (require 'hmac-def))
69 (require 'hex-util)                     ; (decode-hex-string STRING)
70 (require 'md5)                          ; expects (md5 STRING)
71
72 ;; We cannot define this function in md5.el because recent XEmacs provides
73 ;; built-in md5 function and provides feature 'md5 at startup.
74 (if (and (featurep 'xemacs)
75          (fboundp 'md5)
76          (subrp (symbol-function 'md5))
77          (condition-case nil
78              ;; `md5' of XEmacs 21 takes 4th arg CODING (and 5th arg NOERROR).
79              (md5 "" nil nil 'binary)   ; => "fb5d2156096fa1f254352f3cc3fada7e"
80            (error nil)))
81     ;; XEmacs 21.
82     (defun md5-binary (string &optional start end)
83       "Return the MD5 of STRING in binary form."
84       (decode-hex-string (md5 string start end 'binary)))
85   ;; not XEmacs 21 and not DL.
86   (if (not (fboundp 'md5-binary))
87       (defun md5-binary (string)
88         "Return the MD5 of STRING in binary form."
89         (decode-hex-string (md5 string)))))
90
91 (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
92 ;; (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
93
94 (provide 'hmac-md5)
95
96 ;;; hmac-md5.el ends here