module Crypto.Hash
(
HashAlgorithm(..)
, HashFunctionBS
, HashFunctionLBS
, Context
, Digest
, digestToByteString
, digestToHexByteString
, hash
, hashlazy
, hashUpdate
, hashInitAlg
, H.MD2(..)
, H.MD4(..)
, H.MD5(..)
, H.SHA1(..)
, H.SHA224(..)
, H.SHA256(..)
, H.SHA384(..)
, H.SHA512(..)
, H.RIPEMD160(..)
, H.Tiger(..)
, H.SHA3_224(..)
, H.SHA3_256(..)
, H.SHA3_384(..)
, H.SHA3_512(..)
, H.Skein256_224(..)
, H.Skein256_256(..)
, H.Skein512_224(..)
, H.Skein512_256(..)
, H.Skein512_384(..)
, H.Skein512_512(..)
, H.Whirlpool(..)
, HMAC(..)
, hmac
, hmacAlg
) where
import Crypto.Hash.Types
import Data.ByteString (ByteString)
import Data.Byteable
import Data.Bits (xor)
import qualified Data.ByteString as B
import qualified Data.ByteArray.Encoding as B
import qualified Data.ByteString.Lazy as L
import qualified "cryptonite" Crypto.Hash as H
type HashFunctionBS a = ByteString -> Digest a
type HashFunctionLBS a = L.ByteString -> Digest a
hashUpdate :: HashAlgorithm a => Context a -> ByteString -> Context a
hashUpdate ctx b = hashUpdates ctx [b]
hash :: HashAlgorithm a => ByteString -> Digest a
hash bs = hashFinalize $ hashUpdate hashInit bs
hashlazy :: HashAlgorithm a => L.ByteString -> Digest a
hashlazy lbs = hashFinalize $ hashUpdates hashInit (L.toChunks lbs)
digestToHexByteString :: Digest a -> ByteString
digestToHexByteString = B.convertToBase B.Base16 . toBytes
class HashAlgorithm a where
hashBlockSize :: Context a -> Int
hashInit :: Context a
hashUpdates :: Context a -> [ByteString] -> Context a
hashFinalize :: Context a -> Digest a
digestFromByteString :: ByteString -> Maybe (Digest a)
#define DEFINE_INSTANCE(NAME, MODULENAME, BLOCKSIZE) \
instance HashAlgorithm H.NAME where \
{ hashInit = Context $ H.hashInit \
; hashBlockSize ~(Context _) = BLOCKSIZE \
; hashUpdates (Context c) bs = Context $ H.hashUpdates c bs \
; hashFinalize (Context c) = Digest $ H.hashFinalize c \
; digestFromByteString bs = Digest `fmap` H.digestFromByteString bs \
};
#define DEFINE_INSTANCE_LEN(NAME, MODULENAME, LEN, BLOCKSIZE) \
instance HashAlgorithm H.NAME where \
{ hashInit = Context $ H.hashInit \
; hashBlockSize ~(Context _) = BLOCKSIZE \
; hashUpdates (Context c) bs = Context $ H.hashUpdates c bs \
; hashFinalize (Context c) = Digest $ H.hashFinalize c \
; digestFromByteString bs = Digest `fmap` H.digestFromByteString bs \
};
DEFINE_INSTANCE(MD2, MD2, 16)
DEFINE_INSTANCE(MD4, MD4, 64)
DEFINE_INSTANCE(MD5, MD5, 64)
DEFINE_INSTANCE(SHA1, SHA1, 64)
DEFINE_INSTANCE(SHA224, SHA224, 64)
DEFINE_INSTANCE(SHA256, SHA256, 64)
DEFINE_INSTANCE(SHA384, SHA384, 128)
DEFINE_INSTANCE(SHA512, SHA512, 128)
DEFINE_INSTANCE(RIPEMD160, RIPEMD160, 64)
DEFINE_INSTANCE(Whirlpool, Whirlpool, 64)
DEFINE_INSTANCE(Tiger, Tiger, 64)
DEFINE_INSTANCE_LEN(SHA3_224, SHA3, 224, 144)
DEFINE_INSTANCE_LEN(SHA3_256, SHA3, 256, 136)
DEFINE_INSTANCE_LEN(SHA3_384, SHA3, 384, 104)
DEFINE_INSTANCE_LEN(SHA3_512, SHA3, 512, 72)
DEFINE_INSTANCE_LEN(Skein256_224, Skein256, 224, 32)
DEFINE_INSTANCE_LEN(Skein256_256, Skein256, 256, 32)
DEFINE_INSTANCE_LEN(Skein512_224, Skein512, 224, 64)
DEFINE_INSTANCE_LEN(Skein512_256, Skein512, 256, 64)
DEFINE_INSTANCE_LEN(Skein512_384, Skein512, 384, 64)
DEFINE_INSTANCE_LEN(Skein512_512, Skein512, 512, 64)
hashInitAlg :: HashAlgorithm alg => alg -> Context alg
hashInitAlg _ = hashInit
newtype HMAC a = HMAC { hmacGetDigest :: Digest a }
instance Byteable (HMAC a) where
toBytes (HMAC b) = toBytes b
instance Eq (HMAC a) where
(HMAC b1) == (HMAC b2) = constEqBytes (toBytes b1) (toBytes b2)
hmac :: HashAlgorithm a
=> ByteString
-> ByteString
-> HMAC a
hmac secret msg = doHMAC hashInit
where doHMAC :: HashAlgorithm a => Context a -> HMAC a
doHMAC ctxInit = HMAC $ hashF $ B.append opad (toBytes $ 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 toBytes (hashF secret) else secret
pad = B.replicate (fromIntegral blockSize B.length kt) 0
hashF = hashFinalize . hashUpdate ctxInit
blockSize = hashBlockSize ctxInit
hmacAlg :: HashAlgorithm a
=> a
-> ByteString
-> ByteString
-> HMAC a
hmacAlg _ secret msg = hmac secret msg