diff options
author | Benjamin Barenblat <bbaren@google.com> | 2018-09-28 17:20:36 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2018-09-28 17:20:36 -0400 |
commit | 924d0109218f04f4a34bbfe1f5d18b75e1d9a66d (patch) | |
tree | de0d0357d33d774318c4e04b9caad22f0ab05cef /src/Data/HMAC.hs | |
parent | 100fc7dc0c7ce85aad0e413b97a90b98185326ef (diff) |
Stop using newtypes for labeled arguments
It’s getting messy, so switch to a parameters datatype pattern.
Diffstat (limited to 'src/Data/HMAC.hs')
-rw-r--r-- | src/Data/HMAC.hs | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs index 0e5a67f..d27ce7e 100644 --- a/src/Data/HMAC.hs +++ b/src/Data/HMAC.hs @@ -24,7 +24,7 @@ module Data.HMAC ( -- * Computing HMACs HMAC(HMAC) - , hmac + , hmac, HMACParams(..) -- * Cryptographic hash algorithms , Algorithm @@ -35,9 +35,6 @@ module Data.HMAC -- [FIPS 180-4](https://csrc.nist.gov/publications/detail/fips/180/4/final). , sha224, sha256, sha384, sha512 - -- * Keys - , SecretKey(SecretKey) - -- * Error handling , Error @@ -59,7 +56,8 @@ import BTLS.BoringSSL.HMAC import BTLS.BoringSSL.Mem (cryptoMemcmp) import BTLS.Buffer (onBufferOfMaxSize) import BTLS.Result (Error, check) -import BTLS.Types (Algorithm(Algorithm), SecretKey(SecretKey), showHex) +import BTLS.Show (showHex) +import BTLS.Types (Algorithm(Algorithm)) import Data.Digest (md5, sha1, sha224, sha256, sha384, sha512) -- | A hash-based message authentication code. Equality comparisons on this type @@ -76,11 +74,16 @@ instance Eq HMAC where instance Show HMAC where show (HMAC m) = showHex m --- | Creates an HMAC according to the given 'Algorithm'. -hmac :: Algorithm -> SecretKey -> Lazy.ByteString -> Either [Error] HMAC -hmac (Algorithm md) (SecretKey key) bytes = +-- | Creates an HMAC. +hmac :: HMACParams -> Lazy.ByteString -> Either [Error] HMAC +hmac (HMACParams (Algorithm md) key) bytes = unsafeLocalState $ runExceptT $ do ctx <- lift mallocHMACCtx check $ hmacInitEx ctx key md noEngine lift $ mapM_ (hmacUpdate ctx) (ByteString.Lazy.toChunks bytes) lift $ HMAC <$> onBufferOfMaxSize evpMaxMDSize (hmacFinal ctx) + +data HMACParams = HMACParams + { algorithm :: Algorithm + , secretKey :: ByteString + } deriving (Eq, Show) |