aboutsummaryrefslogtreecommitdiff
path: root/src/Data/Digest.hs
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2018-09-01 15:12:30 -0400
committerGravatar Benjamin Barenblat <bbaren@google.com>2018-09-04 11:47:56 -0500
commita31daa3545c0a8cb5f95e88d66cfcee55a7ee925 (patch)
tree9c10af070163f12fe369c185e1b7681db492e5a1 /src/Data/Digest.hs
parent92a90ad43381f6897a93503027d67ac0b1032f3e (diff)
Enable error checking for HMAC computations
Diffstat (limited to 'src/Data/Digest.hs')
-rw-r--r--src/Data/Digest.hs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/Data/Digest.hs b/src/Data/Digest.hs
index 3e6f263..2faf66e 100644
--- a/src/Data/Digest.hs
+++ b/src/Data/Digest.hs
@@ -22,11 +22,12 @@ module Data.Digest
) where
import qualified Data.ByteString.Lazy as ByteString.Lazy
+import Foreign (withForeignPtr)
import Foreign.Marshal.Unsafe (unsafeLocalState)
import BTLS.BoringSSL.Base
import BTLS.BoringSSL.Digest
-import BTLS.BoringSSLPatterns (initUpdateFinalize)
+import BTLS.BoringSSLPatterns (onBufferOfMaxSize)
import BTLS.Types (Algorithm(Algorithm), Digest(Digest))
type LazyByteString = ByteString.Lazy.ByteString
@@ -41,8 +42,10 @@ sha512 = Algorithm evpSHA512
-- | Hashes according to the given 'Algorithm'.
hash :: Algorithm -> LazyByteString -> Digest
-hash (Algorithm md) =
- Digest
- . unsafeLocalState
- . initUpdateFinalize mallocEVPMDCtx initialize evpDigestUpdate evpDigestFinalEx
- where initialize ctx = evpDigestInitEx ctx md noEngine
+hash (Algorithm md) bytes =
+ unsafeLocalState $ do
+ ctxFP <- mallocEVPMDCtx
+ withForeignPtr ctxFP $ \ctx -> do
+ evpDigestInitEx ctx md noEngine
+ mapM_ (evpDigestUpdate ctx) (ByteString.Lazy.toChunks bytes)
+ Digest <$> onBufferOfMaxSize evpMaxMDSize (evpDigestFinalEx ctx)