hmac-md5.el 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; hmac-md5.el --- Compute HMAC-MD5.
  2. ;; Copyright (C) 1999, 2001, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
  4. ;; Keywords: HMAC, RFC2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
  18. ;;
  19. ;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
  20. ;; => "9294727a3638bb1c13f48ef8158bfc9d"
  21. ;;
  22. ;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
  23. ;; => "750c783e6ab0b503eaa86e310a5db738"
  24. ;;
  25. ;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
  26. ;; => "56be34521d144c88dbb8c733f0e8b3f6"
  27. ;;
  28. ;; (encode-hex-string
  29. ;; (hmac-md5
  30. ;; (make-string 50 ?\xcd)
  31. ;; (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
  32. ;; => "697eaf0aca3a3aea3a75164746ffaa79"
  33. ;;
  34. ;; (encode-hex-string
  35. ;; (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
  36. ;; => "56461ef2342edc00f9bab995690efd4c"
  37. ;;
  38. ;; (encode-hex-string
  39. ;; (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
  40. ;; => "56461ef2342edc00f9bab995"
  41. ;;
  42. ;; (encode-hex-string
  43. ;; (hmac-md5
  44. ;; "Test Using Larger Than Block-Size Key - Hash Key First"
  45. ;; (make-string 80 ?\xaa)))
  46. ;; => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
  47. ;;
  48. ;; (encode-hex-string
  49. ;; (hmac-md5
  50. ;; "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
  51. ;; (make-string 80 ?\xaa)))
  52. ;; => "6f630fad67cda0ee1fb1f562db3aa53e"
  53. ;;; Code:
  54. (eval-when-compile (require 'hmac-def))
  55. (require 'hex-util) ; (decode-hex-string STRING)
  56. (require 'md5) ; expects (md5 STRING)
  57. (defun md5-binary (string)
  58. "Return the MD5 of STRING in binary form."
  59. (if (condition-case nil
  60. ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
  61. (md5 "" nil nil 'binary) ; => "d41d8cd98f00b204e9800998ecf8427e"
  62. (wrong-number-of-arguments nil))
  63. (decode-hex-string (md5 string nil nil 'binary))
  64. (decode-hex-string (md5 string))))
  65. (define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
  66. (define-hmac-function hmac-md5-96 md5-binary 64 16 96)
  67. (provide 'hmac-md5)
  68. ;;; hmac-md5.el ends here