module Foundation.Hashing.Hasher
( Hasher(..)
) where
import Basement.Compat.Base
import Basement.IntegralConv
import Foundation.Array (UArray)
import qualified Basement.UArray as A
import Data.Bits
class Hasher st where
type HashResult st
type HashInitParam st
hashNew :: st
hashNewParam :: HashInitParam st -> st
hashEnd :: st -> HashResult st
hashMix8 :: Word8 -> st -> st
hashMix16 :: Word16 -> st -> st
hashMix16 w st = hashMix8 w2 $ hashMix8 w1 st
where
!w1 = integralDownsize (w `unsafeShiftR` 8)
!w2 = integralDownsize w
hashMix32 :: Word32 -> st -> st
hashMix32 w st = hashMix8 w4 $ hashMix8 w3 $ hashMix8 w2 $ hashMix8 w1 st
where
!w1 = integralDownsize (w `unsafeShiftR` 24)
!w2 = integralDownsize (w `unsafeShiftR` 16)
!w3 = integralDownsize (w `unsafeShiftR` 8)
!w4 = integralDownsize w
hashMix64 :: Word64 -> st -> st
hashMix64 w st = hashMix32 w2 $ hashMix32 w1 st
where
!w1 = integralDownsize (w `unsafeShiftR` 32)
!w2 = integralDownsize w
hashMixBytes :: A.PrimType e => UArray e -> st -> st
hashMixBytes ba st = A.foldl' (flip hashMix8) st (A.unsafeRecast ba)