Merge from emacs--devo--0
[gnus] / lisp / hmac-md5.el
1 ;;; hmac-md5.el --- Compute HMAC-MD5.
2
3 ;; Copyright (C) 1999, 2001, 2007, 2008  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 GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
22 ;; 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-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 (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))))
78
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)
81
82 (provide 'hmac-md5)
83
84 ;;; arch-tag: 0ab3f4f6-3d4b-4167-a9fa-635b7fed7f27
85 ;;; hmac-md5.el ends here