Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / net / hmac-md5.el
blobed7aa8605dda6895dd3de9aee95b4d525196d20b
1 ;;; hmac-md5.el --- Compute HMAC-MD5.
3 ;; Copyright (C) 1999, 2001, 2007-2014 Free Software Foundation, Inc.
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: HMAC, RFC2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
8 ;; This file is part of GNU Emacs.
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 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
27 ;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
28 ;; => "9294727a3638bb1c13f48ef8158bfc9d"
30 ;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
31 ;; => "750c783e6ab0b503eaa86e310a5db738"
33 ;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
34 ;; => "56be34521d144c88dbb8c733f0e8b3f6"
36 ;; (encode-hex-string
37 ;; (hmac-md5
38 ;; (make-string 50 ?\xcd)
39 ;; (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
40 ;; => "697eaf0aca3a3aea3a75164746ffaa79"
42 ;; (encode-hex-string
43 ;; (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
44 ;; => "56461ef2342edc00f9bab995690efd4c"
46 ;; (encode-hex-string
47 ;; (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
48 ;; => "56461ef2342edc00f9bab995"
50 ;; (encode-hex-string
51 ;; (hmac-md5
52 ;; "Test Using Larger Than Block-Size Key - Hash Key First"
53 ;; (make-string 80 ?\xaa)))
54 ;; => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
56 ;; (encode-hex-string
57 ;; (hmac-md5
58 ;; "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
59 ;; (make-string 80 ?\xaa)))
60 ;; => "6f630fad67cda0ee1fb1f562db3aa53e"
62 ;;; Code:
64 (eval-when-compile (require 'hmac-def))
65 (require 'hex-util) ; (decode-hex-string STRING)
66 (require 'md5) ; expects (md5 STRING)
68 (defun md5-binary (string)
69 "Return the MD5 of STRING in binary form."
70 (if (condition-case nil
71 ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
72 (md5 "" nil nil 'binary) ; => "d41d8cd98f00b204e9800998ecf8427e"
73 (wrong-number-of-arguments nil))
74 (decode-hex-string (md5 string nil nil 'binary))
75 (decode-hex-string (md5 string))))
77 (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
78 (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
80 (provide 'hmac-md5)
82 ;;; hmac-md5.el ends here