From f55636bd4346ee2124a41c591b310cf2015c0996 Mon Sep 17 00:00:00 2001 From: George Pollard Date: Tue, 6 Mar 2018 18:05:02 +1300 Subject: [PATCH] Add `hmacLazy` for lazy `ByteString`s Modeled off `hashLazy`. --- Crypto/MAC/HMAC.hs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Crypto/MAC/HMAC.hs b/Crypto/MAC/HMAC.hs index 77582e3..6f109cd 100644 --- a/Crypto/MAC/HMAC.hs +++ b/Crypto/MAC/HMAC.hs @@ -12,6 +12,7 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Crypto.MAC.HMAC ( hmac + , hmacLazy , HMAC(..) -- * Incremental , Context(..) @@ -24,11 +25,12 @@ module Crypto.MAC.HMAC import Crypto.Hash hiding (Context) import qualified Crypto.Hash as Hash (Context) import Crypto.Hash.IO -import Crypto.Internal.ByteArray (ScrubbedBytes, ByteArray, ByteArrayAccess) +import Crypto.Internal.ByteArray (ScrubbedBytes, ByteArrayAccess) import qualified Crypto.Internal.ByteArray as B import Data.Memory.PtrMethods import Crypto.Internal.Compat import Crypto.Internal.Imports +import qualified Data.ByteString.Lazy as L -- | Represent an HMAC that is a phantom type with the hash used to produce the mac. -- @@ -39,13 +41,20 @@ newtype HMAC a = HMAC { hmacGetDigest :: Digest a } instance Eq (HMAC a) where (HMAC b1) == (HMAC b2) = B.constEq b1 b2 --- | compute a MAC using the supplied hashing function +-- | Compute a MAC using the supplied hashing function hmac :: (ByteArrayAccess key, ByteArrayAccess message, HashAlgorithm a) => key -- ^ Secret key -> message -- ^ Message to MAC -> HMAC a hmac secret msg = finalize $ updates (initialize secret) [msg] +-- | Compute a MAC using the supplied hashing function, for a lazy input +hmacLazy :: (ByteArrayAccess key, HashAlgorithm a) + => key -- ^ Secret key + -> L.ByteString -- ^ Message to MAC + -> HMAC a +hmacLazy secret msg = finalize $ updates (initialize secret) (L.toChunks msg) + -- | Represent an ongoing HMAC state, that can be appended with 'update' -- and finalize to an HMAC with 'hmacFinalize' data Context hashalg = Context !(Hash.Context hashalg) !(Hash.Context hashalg)