<commit_before>module Raaz.Hash.Sha.Util
    ( padLength64, padding64
    , padLength128,padding128
    ) where

import qualified Data.ByteString as B
import Data.Word
import Foreign.Storable(Storable(..))

import Raaz.Primitives
import Raaz.Types

firstPadByte :: Word8
firstPadByte = 128

-- | Number of bytes in the padding for the first pad byte and the
-- length encoding for a 64-bit length appended hash like
-- SHA1, SHA224, SHA256.
extra64  :: BYTES Int
extra64  = BYTES $ 1 + sizeOf (undefined :: Word64)

-- | Padding length for a 64-bit length appended hash like SHA1,
-- SHA224, SHA256.
padLength64 :: Primitive h => h -> BITS Word64 -> BYTES Int
{-# INLINE padLength64 #-}
padLength64 h l | r >= extra64 = r
                | otherwise    = r + blockSize h
  where lb :: BYTES Int
        lb    = cryptoCoerce l `rem` blockSize h
        r     = blockSize h - lb

-- | Padding string for a 64-bit length appended hash like SHA1,
-- SHA224, SHA256.
padding64 :: Primitive h => h -> BITS Word64 -> B.ByteString
{-# INLINE padding64 #-}

padding64 h l = B.concat [ B.singleton firstPadByte
                         , B.replicate zeros 0
                         , toByteString lBits
                         ]
     where r      = padLength64 h l :: BYTES Int
           zeros  = fromIntegral $ r - extra64
           lBits  = cryptoCoerce l :: BITS Word64BE

-- | Number of bytes in the padding for the first pad byte and the
-- length encoding for a 128-bit length appended hash like
-- SHA384,SHA512.
extra128 :: BYTES Int
extra128 = BYTES $ 1 + 2*sizeOf (undefined :: Word64)

-- | Padding length for a 128-bit length appended hash like
-- SHA384,SHA512.
padLength128 :: Primitive h => h -> BITS Word64 -> BYTES Int
{-# INLINE padLength128 #-}
padLength128 h l | r >= extra128 = r
                 | otherwise     = r + blockSize h
  where lb :: BYTES Int
        lb    = cryptoCoerce l `rem` blockSize h
        r     = blockSize h - lb

-- | Padding string for a 128-bit length appended hash like
-- SHA384,SHA512.
padding128 :: Primitive h => h -> BITS Word64 -> B.ByteString
{-# INLINE padding128 #-}
padding128 h l = B.concat [ B.singleton firstPadByte
                          , B.replicate totalZeros 0
                          , toByteString lBits
                          ]
     where r            = padLength128 h l :: BYTES Int
           BYTES zeros  = r - extra128
           totalZeros   = zeros + sizeOf (undefined :: Word64BE)
           lBits        = cryptoCoerce l :: BITS Word64BE<commit_msg>refactored the padLength and shaPadding<commit_after>module Raaz.Hash.Sha.Util
       ( shaPadLength, shaPadding
       ) where

import Data.ByteString      hiding  ( length            )
import Data.Monoid                  ( (<>)              )
import Data.Word
import Prelude              hiding  ( length, replicate )

import Raaz.Primitives
import Raaz.Types
import Raaz.Util.ByteString ( length   )

firstPadByte :: Word8
firstPadByte = 128

-- | This computes the padding length for the sha family of
-- hashes.
shaPadLength :: Primitive prim
          => BYTES Int      -- ^ The bytes need to encode the
                            -- message length
          -> prim           -- ^ The primitive
          -> BITS Word64    -- ^ The length of the message
          -> BYTES Int
{-# INLINEABLE shaPadLength #-}
shaPadLength lenSize h l
  | r >= lenSize + 1 = r
  | otherwise        = r + blockSize h
  where lb :: BYTES Int
        lb = cryptoCoerce l `rem` blockSize h
        r  = blockSize h - lb

-- | This computes the padding for the sha family of hashes.
shaPadding :: Primitive prim
           => BYTES Int      -- ^ The bytes need to encode the
                             -- message length
           -> prim           -- ^ The primitive
           -> BITS Word64  -- ^ The length of the message
           -> ByteString
{-# INLINEABLE shaPadding #-}
shaPadding lenSize prim lBits =  singleton firstPadByte
                              <> replicate zeros 0
                              <> lPad
     where pLen        = shaPadLength lenSize prim lBits
           lPad        = toByteString l
           l           = cryptoCoerce lBits :: BITS Word64BE
           BYTES zeros = pLen - length lPad - 1
{'fraction_non_alphanumeric': 0.08194019307746644, 'fraction_numerical': 0.037438191664704495, 'mean_word_length': 1.8188453881884539, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7126637', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1190, 'n_words': 440}