module Crypto.MAC.HMAC
( hmac
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Bits (xor)
hmac :: (ByteString -> ByteString)
-> Int
-> ByteString
-> ByteString
-> ByteString
hmac hashF blockSize secret msg = hashF $ B.append opad (hashF $ 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 hashF secret else secret
pad = B.replicate (fromIntegral blockSize B.length kt) 0