From 924d0109218f04f4a34bbfe1f5d18b75e1d9a66d Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Fri, 28 Sep 2018 17:20:36 -0400 Subject: Stop using newtypes for labeled arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s getting messy, so switch to a parameters datatype pattern. --- src/Data/HMAC.hs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/Data/HMAC.hs') 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) -- cgit v1.2.3