Skip to content
Browse files

Add simple HMAC algorithm

  • Loading branch information...
1 parent 9dff731 commit 0a04e72cd12ab1fd3fc94212c7483d7d32d8617d @vincenthz committed Oct 23, 2011
Showing with 28 additions and 0 deletions.
  1. +27 −0 Crypto/MAC/HMAC.hs
  2. +1 −0 cryptohash.cabal
View
27 Crypto/MAC/HMAC.hs
@@ -0,0 +1,27 @@
+-- |
+-- Module : Crypto.MAC.HMAC
+-- License : BSD-style
+-- Maintainer : Vincent Hanquez <vincent@snarc.org>
+-- Stability : experimental
+-- Portability : unknown
+--
+-- provide the HMAC (Hash based Message Authentification Code) base algorithm.
+-- http://en.wikipedia.org/wiki/HMAC
+--
+module Crypto.MAC.HMAC
+ ( hmac
+ ) where
+
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as B
+import Data.Bits (xor)
+
+-- | compute a MAC using the supplied hashing function
+hmac :: (ByteString -> ByteString) -> Int -> ByteString -> ByteString -> ByteString
+hmac f blockSize secret msg = f $ B.append opad (f $ B.append ipad msg) where
+ opad = B.map (xor 0x5c) k'
+ ipad = B.map (xor 0x36) k'
+
+ k' = B.append kt pad
+ kt = if B.length secret > fromIntegral blockSize then f secret else secret
+ pad = B.replicate (fromIntegral blockSize - B.length kt) 0
View
1 cryptohash.cabal
@@ -51,6 +51,7 @@ Library
Crypto.Hash.Skein256
Crypto.Hash.Skein512
Crypto.Hash.Tiger
+ Crypto.MAC.HMAC
ghc-options: -Wall -O2 -optc-O3 -fno-cse
C-sources: cbits/sha1.c
cbits/sha256.c

0 comments on commit 0a04e72

Please sign in to comment.
Something went wrong with that request. Please try again.