module Crypto.Hash.Tsuraan.Blake2.Parallel
( Ctx
, init
, init_key
, update
, finalize
, hash
, hash_key
) where
import Data.ByteString ( ByteString )
import System.IO.Unsafe ( unsafePerformIO )
import Foreign.C ( CInt(..) )
import Foreign.Ptr ( Ptr )
import Foreign.Storable ( Storable(..) )
import Data.Word ( Word8 )
import Crypto.Hash.Tsuraan.Blake2.Internal ( BlakeState(..), runHasher, runInit, runInitKey, runUpdate, runFinalize )
import Prelude hiding ( init )
data Blake2bpState
newtype Ctx = Ctx (BlakeState Blake2bpState) deriving ( Show )
instance Storable Blake2bpState where
sizeOf _ = unsafePerformIO blake2bp_size
alignment _ = 64
peek _ = error "no peek"
poke _a _b = error "no poke"
foreign import ccall "blake2.h blake2bp" blake2bp
:: Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int -> Int -> Int -> IO CInt
foreign import ccall "blake2.h blake2bp_init" blake2bp_init
:: Ptr Blake2bpState -> Int -> IO CInt
foreign import ccall "blake2.h blake2bp_init_key" blake2bp_init_key
:: Ptr Blake2bpState -> Int -> Ptr Word8 -> Int -> IO CInt
foreign import ccall "blake2.h blake2bp_update" blake2bp_update
:: Ptr Blake2bpState -> Ptr Word8 -> Int -> IO CInt
foreign import ccall "blake2.h blake2bp_final" blake2bp_final
:: Ptr Blake2bpState -> Ptr Word8 -> Int -> IO CInt
foreign import ccall "alloc.h blake2bp_size" blake2bp_size
:: IO Int
init :: Int
-> IO Ctx
init outlen = Ctx `fmap` runInit blake2bp_init outlen
init_key :: ByteString
-> Int
-> IO Ctx
init_key key outlen = Ctx `fmap` runInitKey blake2bp_init_key key outlen
update :: Ctx
-> ByteString
-> IO ()
update (Ctx ptr) bs = runUpdate blake2bp_update ptr bs
finalize :: Ctx
-> IO ByteString
finalize (Ctx state) = runFinalize blake2bp_final state
hash_key :: ByteString
-> Int
-> ByteString
-> ByteString
hash_key key hashlen bytes = runHasher blake2bp key hashlen bytes
hash :: Int
-> ByteString
-> ByteString
hash = hash_key ""