diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-08-16 00:33:19 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2011-08-16 00:33:19 +0200 |
commit | 34a9c6bef7db450e0d8ea58dbc04740e3b261b12 (patch) | |
tree | 92fbf35538d24102245e74a94673ae62a0628fb1 /hmac.rkt | |
parent | 3aaa523f41df21b493133c4f3580c4cfed1611ff (diff) |
Implement HMAC.
Diffstat (limited to 'hmac.rkt')
-rw-r--r-- | hmac.rkt | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/hmac.rkt b/hmac.rkt new file mode 100644 index 0000000..6ff03cf --- /dev/null +++ b/hmac.rkt @@ -0,0 +1,62 @@ +#lang typed/racket +;;; Copyright 2011, Matthias Andreas Benkard. +;;; +;;;----------------------------------------------------------------------------- +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU Affero General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details. +;;; +;;; You should have received a copy of the GNU Affero General Public License +;;; along with this program. If not, see <http://www.gnu.org/licenses/>. +;;;----------------------------------------------------------------------------- +;;; + +(require "util.rkt") + +(provide: [hmac ((Bytes -> Exact-Nonnegative-Integer) + Exact-Nonnegative-Integer + Exact-Nonnegative-Integer + Bytes + Bytes + -> + Exact-Nonnegative-Integer)]) + +;; Example: +;; +;; (hmac whirlpool 64 64 #"<secret key>" #"hello") +;; +(define (hmac hashfn blocksize hashsize key msg) + (let ([opad (make-bytes blocksize #x5c)] + [ipad (make-bytes blocksize #x36)] + [padded-key + (pad-bytes (if (> (bytes-length key) blocksize) + (integer->bytes (hashfn key)) + key) + blocksize + #x0 + 'left)]) + (hashfn (bytes-append (integer->bytes/size + (bitwise-xor (bytes->integer opad) + (bytes->integer key)) + blocksize) + (integer->bytes/size + (hashfn + (bytes-append + (integer->bytes/size + (bitwise-xor (bytes->integer ipad) + (bytes->integer key)) + blocksize) + msg)) + hashsize))))) + +#; +(begin + (require "whirlpool.rkt") + (printf "~x~%" (hmac whirlpool 64 64 #"" #""))) + |