sha512

Pure Haskell SHA-512, HMAC-SHA512 (docs.ppad.tech/sha512).
git clone git://git.ppad.tech/sha512.git
Log | Files | Refs | README | LICENSE

commit 2084475622d31bc6b0f954c76aa8b605e1abdc32
parent 99ea49e18b2f65aa2ba341bf8ddc50a07f416341
Author: Jared Tobin <jared@jtobin.io>
Date:   Sun, 13 Oct 2024 13:30:16 +0400

lib: don't call hmac_lazy in strict

Denoises profiling.

Diffstat:
Mlib/Crypto/Hash/SHA512.hs | 23++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/lib/Crypto/Hash/SHA512.hs b/lib/Crypto/Hash/SHA512.hs @@ -495,6 +495,10 @@ hash_lazy bl = cat (go iv (pad_lazy bl)) where -- HMAC ----------------------------------------------------------------------- -- https://datatracker.ietf.org/doc/html/rfc2104#section-2 +data KeyAndLen = KeyAndLen + {-# UNPACK #-} !BS.ByteString + {-# UNPACK #-} !Int + -- | Produce a message authentication code for a strict bytestring, -- based on the provided (strict, bytestring) key, via SHA-512. -- @@ -509,11 +513,20 @@ hmac :: BS.ByteString -- ^ key -> BS.ByteString -- ^ text -> BS.ByteString -hmac k = hmac_lazy k . BL.fromStrict - -data KeyAndLen = KeyAndLen - {-# UNPACK #-} !BS.ByteString - {-# UNPACK #-} !Int +hmac mk text = + let step1 = k <> BS.replicate (128 - lk) 0x00 + step2 = BS.map (B.xor 0x36) step1 + step3 = step2 <> text + step4 = hash step3 + step5 = BS.map (B.xor 0x5C) step1 + step6 = step5 <> step4 + in hash step6 + where + !(KeyAndLen k lk) = + let l = BS.length mk + in if l > 128 + then KeyAndLen (hash mk) 64 + else KeyAndLen mk l -- | Produce a message authentication code for a lazy bytestring, based -- on the provided (strict, bytestring) key, via SHA-512.