{-# language DataKinds #-}
{-# language MagicHash #-}
{-# language UnliftedFFITypes #-}

module Hash.Sha1
  ( boundedBuilder
  , byteArrayN
  ) where

import Control.Monad.ST (ST)
import Control.Monad.ST.Run (runByteArrayST)
import Data.Bytes.Builder.Bounded as BB
import Data.Bytes.Builder.Bounded.Unsafe as BBU
import Data.Bytes.Types (Bytes(Bytes),ByteArrayN(ByteArrayN))
import Data.Primitive (ByteArray(..),MutableByteArray(..))
import GHC.Exts (Int(I#),Int#,MutableByteArray#,ByteArray#)
import GHC.IO (unsafeIOToST)

import qualified Data.Primitive as PM

foreign import ccall unsafe "sha1.h hs_cryptohash_sha1_onepass"
  c_hash :: MutableByteArray# s -> Int# -> ByteArray# -> Int# -> Int# -> IO ()

performHash :: MutableByteArray s -> Int -> ByteArray -> Int -> Int -> ST s ()
performHash :: forall s.
MutableByteArray s -> Int -> ByteArray -> Int -> Int -> ST s ()
performHash (MutableByteArray MutableByteArray# s
x) (I# Int#
a) (ByteArray ByteArray#
y) (I# Int#
b) (I# Int#
c) =
  forall a s. IO a -> ST s a
unsafeIOToST (forall s.
MutableByteArray# s -> Int# -> ByteArray# -> Int# -> Int# -> IO ()
c_hash MutableByteArray# s
x Int#
a ByteArray#
y Int#
b Int#
c)

-- | Hash the byte sequence, returning the result as a builder.
boundedBuilder :: Bytes -> BB.Builder 20
boundedBuilder :: Bytes -> Builder 20
boundedBuilder (Bytes ByteArray
arr Int
off Int
len) = forall (n :: Nat).
(forall s. MutableByteArray s -> Int -> ST s Int) -> Builder n
BBU.construct
  (\MutableByteArray s
buf Int
ix -> do
    forall s.
MutableByteArray s -> Int -> ByteArray -> Int -> Int -> ST s ()
performHash MutableByteArray s
buf Int
ix ByteArray
arr Int
off Int
len
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
ix forall a. Num a => a -> a -> a
+ Int
20)
  )

-- | Hash the byte sequence, returning the result as a byte array
-- known to have exactly 20 bytes.
byteArrayN :: Bytes -> ByteArrayN 20
byteArrayN :: Bytes -> ByteArrayN 20
byteArrayN (Bytes ByteArray
arr Int
off Int
len) = forall (n :: Nat). ByteArray -> ByteArrayN n
ByteArrayN forall a b. (a -> b) -> a -> b
$ (forall s. ST s ByteArray) -> ByteArray
runByteArrayST forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
20
  forall s.
MutableByteArray s -> Int -> ByteArray -> Int -> Int -> ST s ()
performHash MutableByteArray s
dst Int
0 ByteArray
arr Int
off Int
len
  forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
dst