File: hmac-sha1.el

package info (click to toggle)
flim 1%3A1.14.9%2B0.20201117-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 864 kB
  • sloc: lisp: 7,300; makefile: 86; sh: 86
file content (77 lines) | stat: -rw-r--r-- 2,706 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; hmac-sha1.el --- Compute HMAC-SHA1.  -*- lexical-binding: t -*-

;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.

;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
;; Keywords: HMAC, RFC 2104, HMAC-SHA1, SHA1, Cancel-Lock

;; This file is part of FLIM (Faithful Library about Internet Message).

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
;;
;; (encode-hex-string (hmac-sha1 "Hi There" (make-string 20 ?\x0b)))
;;  => "b617318655057264e28bc0b6fb378c8ef146be00"
;;
;; (encode-hex-string (hmac-sha1 "what do ya want for nothing?" "Jefe"))
;;  => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
;;
;; (encode-hex-string (hmac-sha1 (make-string 50 ?\xdd) (make-string 20 ?\xaa)))
;;  => "125d7342b9ac11cd91a39af48aa17b4f63f175d3"
;;
;; (encode-hex-string
;;  (hmac-sha1
;;   (make-string 50 ?\xcd)
;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
;;  => "4c9007f4026250c6bc8414f9bf50c86c2d7235da"
;;
;; (encode-hex-string
;;  (hmac-sha1 "Test With Truncation" (make-string 20 ?\x0c)))
;;  => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"
;;
;; (encode-hex-string
;;  (hmac-sha1-96 "Test With Truncation" (make-string 20 ?\x0c)))
;;  => "4c1a03424b55e07fe7f27be1"
;;
;; (encode-hex-string
;;  (hmac-sha1
;;   "Test Using Larger Than Block-Size Key - Hash Key First"
;;   (make-string 80 ?\xaa)))
;;  => "aa4ae5e15272d00e95705637ce8a3b55ed402112"
;;
;; (encode-hex-string
;;  (hmac-sha1
;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
;;   (make-string 80 ?\xaa)))
;;  => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91"

;;; Code:

(eval-when-compile (require 'hmac-def))

(defun sha1-binary (string)
  "Return the SHA1 of STRING in binary form."
  (secure-hash 'sha1 string nil nil t))

(define-hmac-function hmac-sha1 sha1-binary 64 20) ; => (hmac-sha1 TEXT KEY)
(define-hmac-function hmac-sha1-96 sha1-binary 64 20 96)

(provide 'hmac-sha1)

;;; hmac-sha1.el ends here