<commit_before>-- |
-- Module      : Crypto.MAC.HMAC
-- License     : BSD-style
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : unknown
--
-- provide the HMAC (Hash based Message Authentification Code) base algorithm.
-- http://en.wikipedia.org/wiki/HMAC
--
module Crypto.MAC.HMAC
    ( hmac
    ) where

import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Bits (xor)

-- | compute a MAC using the supplied hashing function
hmac :: (ByteString -> ByteString) -> Int -> ByteString -> ByteString -> ByteString
hmac hashF blockSize secret msg = hashF $ B.append opad (hashF $ B.append ipad msg)
    where opad = B.map (xor 0x5c) k'
          ipad = B.map (xor 0x36) k'

          k'  = B.append kt pad
          kt  = if B.length secret > fromIntegral blockSize then hashF secret else secret
          pad = B.replicate (fromIntegral blockSize - B.length kt) 0<commit_msg>Slightly more informative Haddocks<commit_after>-- |
-- Module      : Crypto.MAC.HMAC
-- License     : BSD-style
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : unknown
--
-- provide the HMAC (Hash based Message Authentification Code) base algorithm.
-- <http://en.wikipedia.org/wiki/HMAC>
--
module Crypto.MAC.HMAC
    ( hmac
    ) where

import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Bits (xor)

-- | compute a MAC using the supplied hashing function
hmac :: (ByteString -> ByteString) -- ^ hash function
     -> Int -- ^ block size
     -> ByteString -- ^ secret
     -> ByteString -- ^ message
     -> ByteString
hmac hashF blockSize secret msg = hashF $ B.append opad (hashF $ B.append ipad msg)
    where opad = B.map (xor 0x5c) k'
          ipad = B.map (xor 0x36) k'

          k'  = B.append kt pad
          kt  = if B.length secret > fromIntegral blockSize then hashF secret else secret
          pad = B.replicate (fromIntegral blockSize - B.length kt) 0
{'fraction_non_alphanumeric': 0.11341772151898734, 'fraction_numerical': 0.0060759493670886075, 'mean_word_length': 2.5927272727272728, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4288264', 'n_tokens_mistral': 637, 'n_tokens_neox': 614, 'n_words': 204}