Merge from emacs--rel--22
[emacs.git] / lisp / gnus / hmac-md5.el
blob21fc91992ad7380d62e9157ae73a91d00bf35245
1 ;;; hmac-md5.el --- Compute HMAC-MD5.
3 ;; Copyright (C) 1999, 2001 Free Software Foundation, Inc.
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: HMAC, RFC 2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 3, or
13 ;; (at your option) any later version.
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.
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.
25 ;;; Commentary:
27 ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
29 ;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
30 ;; => "9294727a3638bb1c13f48ef8158bfc9d"
32 ;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
33 ;; => "750c783e6ab0b503eaa86e310a5db738"
35 ;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
36 ;; => "56be34521d144c88dbb8c733f0e8b3f6"
38 ;; (encode-hex-string
39 ;; (hmac-md5
40 ;; (make-string 50 ?\xcd)
41 ;; (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
42 ;; => "697eaf0aca3a3aea3a75164746ffaa79"
44 ;; (encode-hex-string
45 ;; (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
46 ;; => "56461ef2342edc00f9bab995690efd4c"
48 ;; (encode-hex-string
49 ;; (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
50 ;; => "56461ef2342edc00f9bab995"
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"
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"
64 ;;; Code:
66 (eval-when-compile (require 'hmac-def))
67 (require 'hex-util) ; (decode-hex-string STRING)
68 (require 'md5) ; expects (md5 STRING)
70 (defun md5-binary (string)
71 "Return the MD5 of STRING in binary form."
72 (if (condition-case nil
73 ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
74 (md5 "" nil nil 'binary) ; => "d41d8cd98f00b204e9800998ecf8427e"
75 (wrong-number-of-arguments nil))
76 (decode-hex-string (md5 string nil nil 'binary))
77 (decode-hex-string (md5 string))))
79 (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
80 (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
82 (provide 'hmac-md5)
84 ;;; arch-tag: 0ab3f4f6-3d4b-4167-a9fa-635b7fed7f27
85 ;;; hmac-md5.el ends here