9c936d05ee4d877ba678d54cb2ff95a14f4c068c
[elisp/flim.git] / hmac-md5.el
1 ;;; hmac-md5.el --- Compute HMAC-MD5.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
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 ;; (encode-hex-string
50 ;;  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
51 ;;  => "56461ef2342edc00f9bab995"
52 ;;
53 ;; (encode-hex-string
54 ;;  (hmac-md5
55 ;;   "Test Using Larger Than Block-Size Key - Hash Key First"
56 ;;   (make-string 80 ?\xaa)))
57 ;;  => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
58 ;;
59 ;; (encode-hex-string
60 ;;  (hmac-md5
61 ;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
62 ;;   (make-string 80 ?\xaa)))
63 ;;  => "6f630fad67cda0ee1fb1f562db3aa53e"
64
65 ;;; Code:
66
67 (eval-when-compile (require 'hmac-def))
68 (require 'hex-util)                     ; (decode-hex-string STRING)
69 (require 'md5)                          ; expects (md5 STRING)
70
71 ;; We cannot define this function in md5.el because recent XEmacs provides
72 ;; built-in md5 function and provides feature 'md5 at startup.
73 (if (and (featurep 'xemacs)
74          (fboundp 'md5)
75          (subrp (symbol-function 'md5))
76          (condition-case nil
77              ;; `md5' of XEmacs 21 takes 4th arg CODING (and 5th arg NOERROR).
78              (md5 "" nil nil 'binary)   ; => "fb5d2156096fa1f254352f3cc3fada7e"
79            (error nil)))
80     ;; XEmacs 21.
81     (defun md5-binary (string &optional start end)
82       "Return the MD5 of STRING in binary form."
83       (decode-hex-string (md5 string start end 'binary)))
84   ;; not XEmacs 21 and not DL.
85   (if (not (fboundp 'md5-binary))
86       (defun md5-binary (string)
87         "Return the MD5 of STRING in binary form."
88         (decode-hex-string (md5 string)))))
89
90 (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
91 ;; (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
92
93 (provide 'hmac-md5)
94
95 ;;; hmac-md5.el ends here