move lowlevel implementation of hashes to byteArrays

This commit is contained in:
Vincent Hanquez 2015-04-24 13:30:58 +01:00
parent ec4e0c4ed9
commit 6722a02a74
35 changed files with 633 additions and 560 deletions

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.Kekkak
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Foreign.Storable (peek) import Foreign.Storable (peek)
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen) internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_kekkak_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_kekkak_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_kekkak_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_kekkak_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> create digestSize (c_kekkak_finalize ptr) peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_kekkak_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.MD2
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md2_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md2_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md2_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md2_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_md2_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_md2_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.MD4
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md4_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md4_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md4_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md4_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_md4_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_md4_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.MD5
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md5_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md5_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md5_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_md5_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_md5_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_md5_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.RIPEMD160
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 128 internalInitAt internalInit = Ctx `fmap` bytesAlloc 128 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_ripemd160_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_ripemd160_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_ripemd160_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_ripemd160_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_ripemd160_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_ripemd160_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA1
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha1_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha1_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha1_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha1_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_sha1_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_sha1_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA224
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha224_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha224_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha224_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha224_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_sha224_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_sha224_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA256
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha256_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha256_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha256_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha256_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_sha256_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_sha256_finalize ptr)

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.SHA3
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Foreign.Storable (peek) import Foreign.Storable (peek)
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen) internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha3_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha3_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha3_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha3_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> create digestSize (c_sha3_finalize ptr) peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_sha3_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA384
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha384_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha384_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha384_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha384_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_sha384_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_sha384_finalize ptr)

View File

@ -20,18 +20,17 @@ module Crypto.Hash.Internal.SHA512
, internalUpdateUnsafe , internalUpdateUnsafe
, internalFinalize , internalFinalize
-- * Context copy and creation -- * Context copy and creation
, withCtxNew
, withCtxCopy , withCtxCopy
, withCtxNewThrow , withCtxNewThrow
, withCtxThrow , withCtxThrow
, withCtxNew
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -75,18 +74,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha512_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha512_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha512_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_sha512_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_sha512_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_sha512_finalize ptr)

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.Skein256
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Foreign.Storable (peek) import Foreign.Storable (peek)
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 96 (internalInitAt hashlen) internalInit hashlen = Ctx `fmap` bytesAlloc 96 (internalInitAt hashlen)
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein256_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_skein256_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein256_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_skein256_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> create digestSize (c_skein256_finalize ptr) peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_skein256_finalize ptr)

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.Skein512
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Foreign.Storable (peek) import Foreign.Storable (peek)
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 160 (internalInitAt hashlen) internalInit hashlen = Ctx `fmap` bytesAlloc 160 (internalInitAt hashlen)
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein512_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_skein512_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein512_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_skein512_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> create digestSize (c_skein512_finalize ptr) peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_skein512_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.Tiger
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_tiger_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_tiger_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_tiger_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_tiger_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_tiger_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_tiger_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.Whirlpool
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 168 internalInitAt internalInit = Ctx `fmap` bytesAlloc 168 internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_whirlpool_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_whirlpool_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_whirlpool_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_whirlpool_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_whirlpool_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_whirlpool_finalize ptr)

View File

@ -14,21 +14,21 @@ module Crypto.Hash.Kekkak
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Int -> Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: Int -> ByteString -> ByteString , hash
, hashlazy -- :: Int -> ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.Kekkak import Crypto.Hash.Internal.Kekkak
{-# NOINLINE init #-} {-# NOINLINE init #-}
-- | init a context where -- | init a context where
@ -38,35 +38,39 @@ init hashlen = unsafeDoIO (internalInit hashlen)
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -- ^ algorithm hash size in bits hash :: (ByteArray digest, ByteArrayAccess ba)
-> ByteString -- ^ the data to hash => Int -- ^ algorithm hash size in bits
-> ByteString -- ^ the digest output -> ba -- ^ the data to hash
-> digest -- ^ the digest output
hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -- ^ algorithm hash size in bits hashlazy :: ByteArray digest
=> Int -- ^ algorithm hash size in bits
-> L.ByteString -- ^ the data to hash as a lazy bytestring -> L.ByteString -- ^ the data to hash as a lazy bytestring
-> ByteString -- ^ the digest output -> digest -- ^ the digest output
hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.MD2
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.MD2 import Crypto.Hash.Internal.MD2
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.MD4
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.MD4 import Crypto.Hash.Internal.MD4
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.MD5
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.MD5 import Crypto.Hash.Internal.MD5
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.RIPEMD160
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.RIPEMD160 import Crypto.Hash.Internal.RIPEMD160
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA1
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA1 import Crypto.Hash.Internal.SHA1
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA224
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA224 import Crypto.Hash.Internal.SHA224
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA256
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA256 import Crypto.Hash.Internal.SHA256
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA3
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Int -> Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: Int -> ByteString -> ByteString , hash
, hashlazy -- :: Int -> ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA3 import Crypto.Hash.Internal.SHA3
{-# NOINLINE init #-} {-# NOINLINE init #-}
-- | init a context where -- | init a context where
@ -38,35 +38,39 @@ init hashlen = unsafeDoIO (internalInit hashlen)
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -- ^ algorithm hash size in bits hash :: (ByteArray digest, ByteArrayAccess ba)
-> ByteString -- ^ the data to hash => Int -- ^ algorithm hash size in bits
-> ByteString -- ^ the digest output -> ba -- ^ the data to hash
-> digest -- ^ the digest output
hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -- ^ algorithm hash size in bits hashlazy :: ByteArray digest
=> Int -- ^ algorithm hash size in bits
-> L.ByteString -- ^ the data to hash as a lazy bytestring -> L.ByteString -- ^ the data to hash as a lazy bytestring
-> ByteString -- ^ the digest output -> digest -- ^ the digest output
hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA384
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA384 import Crypto.Hash.Internal.SHA384
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.SHA512
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.SHA512 import Crypto.Hash.Internal.SHA512
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -20,17 +20,15 @@ module Crypto.Hash.SHA512t
, hashlazy -- :: ByteString -> ByteString , hashlazy -- :: ByteString -> ByteString
) where ) where
import Prelude hiding (init) import Prelude hiding (init, take)
import Data.List (foldl') import Data.List (foldl')
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import qualified Crypto.Hash.SHA512 as SHA512 import qualified Crypto.Hash.SHA512 as SHA512
import Crypto.Internal.Compat import Crypto.Internal.Compat
--import qualified Crypto.Hash.Internal.SHA512 as SHA512 import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, take)
import qualified Crypto.Hash.Internal.SHA512t as SHA512t import qualified Crypto.Hash.Internal.SHA512t as SHA512t
import Crypto.Hash.Internal.SHA512 (withCtxNew) import Crypto.Hash.Internal.SHA512 (withCtxNew)
-- | SHA512 Context with variable size output -- | SHA512 Context with variable size output
data Ctx = Ctx !Int !SHA512.Ctx data Ctx = Ctx !Int !SHA512.Ctx
@ -40,17 +38,17 @@ init :: Int -> Ctx
init t = Ctx t $ unsafeDoIO $ withCtxNew $ \ptr -> SHA512t.internalInitAt t ptr init t = Ctx t $ unsafeDoIO $ withCtxNew $ \ptr -> SHA512t.internalInitAt t ptr
-- | update a context with a bytestring -- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx update :: ByteArrayAccess ba => Ctx -> ba -> Ctx
update (Ctx t ctx) d = Ctx t (SHA512.update ctx d) update (Ctx t ctx) d = Ctx t (SHA512.update ctx d)
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize (Ctx sz ctx) = B.take (sz `div` 8) (SHA512.finalize ctx) finalize (Ctx sz ctx) = take (sz `div` 8) (SHA512.finalize ctx)
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -> ByteString -> ByteString hash :: (ByteArrayAccess ba, ByteArray digest) => Int -> ba -> digest
hash t = finalize . update (init t) hash t = finalize . update (init t)
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -> L.ByteString -> ByteString hashlazy :: ByteArray digest => Int -> L.ByteString -> digest
hashlazy t = finalize . foldl' update (init t) . L.toChunks hashlazy t = finalize . foldl' update (init t) . L.toChunks

View File

@ -14,21 +14,21 @@ module Crypto.Hash.Skein256
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Int -> Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: Int -> ByteString -> ByteString , hash
, hashlazy -- :: Int -> ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.Skein256 import Crypto.Hash.Internal.Skein256
{-# NOINLINE init #-} {-# NOINLINE init #-}
-- | init a context where -- | init a context where
@ -38,35 +38,39 @@ init hashlen = unsafeDoIO (internalInit hashlen)
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -- ^ algorithm hash size in bits hash :: (ByteArray digest, ByteArrayAccess ba)
-> ByteString -- ^ the data to hash => Int -- ^ algorithm hash size in bits
-> ByteString -- ^ the digest output -> ba -- ^ the data to hash
-> digest -- ^ the digest output
hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -- ^ algorithm hash size in bits hashlazy :: ByteArray digest
=> Int -- ^ algorithm hash size in bits
-> L.ByteString -- ^ the data to hash as a lazy bytestring -> L.ByteString -- ^ the data to hash as a lazy bytestring
-> ByteString -- ^ the digest output -> digest -- ^ the digest output
hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.Skein512
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Int -> Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: Int -> ByteString -> ByteString , hash
, hashlazy -- :: Int -> ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.Skein512 import Crypto.Hash.Internal.Skein512
{-# NOINLINE init #-} {-# NOINLINE init #-}
-- | init a context where -- | init a context where
@ -38,35 +38,39 @@ init hashlen = unsafeDoIO (internalInit hashlen)
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -- ^ algorithm hash size in bits hash :: (ByteArray digest, ByteArrayAccess ba)
-> ByteString -- ^ the data to hash => Int -- ^ algorithm hash size in bits
-> ByteString -- ^ the digest output -> ba -- ^ the data to hash
-> digest -- ^ the digest output
hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -- ^ algorithm hash size in bits hashlazy :: ByteArray digest
=> Int -- ^ algorithm hash size in bits
-> L.ByteString -- ^ the data to hash as a lazy bytestring -> L.ByteString -- ^ the data to hash as a lazy bytestring
-> ByteString -- ^ the digest output -> digest -- ^ the digest output
hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.Tiger
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.Tiger import Crypto.Hash.Internal.Tiger
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.Whirlpool
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.Whirlpool import Crypto.Hash.Internal.Whirlpool
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.%%MODULENAME%%
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Foreign.Storable (peek) import Foreign.Storable (peek)
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc %%SIZECTX%% (internalInitAt hashlen) internalInit hashlen = Ctx `fmap` bytesAlloc %%SIZECTX%% (internalInitAt hashlen)
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_%%HASHNAME%%_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_%%HASHNAME%%_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_%%HASHNAME%%_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_%%HASHNAME%%_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> create digestSize (c_%%HASHNAME%%_finalize ptr) peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_%%HASHNAME%%_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.%%MODULENAME%%
, withCtxThrow , withCtxThrow
) where ) where
import Foreign.Ptr import Foreign.Ptr
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import qualified Crypto.Internal.ByteArray as B
import Data.ByteString.Internal (create) import Data.Word
import Data.Word import Crypto.Internal.Memory
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc %%SIZECTX%% internalInitAt internalInit = Ctx `fmap` bytesAlloc %%SIZECTX%% internalInitAt
-- | Update a context in place -- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO () internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d = internalUpdate ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_%%HASHNAME%%_update ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_%%HASHNAME%%_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call. -- | Update a context in place using an unsafe foreign function call.
-- --
-- It is faster than `internalUpdate`, but will block the haskell runtime. -- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large. -- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: Ptr Ctx -> ByteString -> IO () internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d = internalUpdateUnsafe ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_%%HASHNAME%%_update_unsafe ptr (castPtr cs) (fromIntegral len)) B.withByteArray d $ \cs -> c_%%HASHNAME%%_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place -- | Finalize a context in place
internalFinalize :: Ptr Ctx -> IO ByteString internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = create digestSize (c_%%HASHNAME%%_finalize ptr) internalFinalize ptr = B.alloc digestSize (c_%%HASHNAME%%_finalize ptr)

View File

@ -14,21 +14,21 @@ module Crypto.Hash.%%MODULENAME%%
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Int -> Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: Int -> ByteString -> ByteString , hash
, hashlazy -- :: Int -> ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.%%MODULENAME%% import Crypto.Hash.Internal.%%MODULENAME%%
{-# NOINLINE init #-} {-# NOINLINE init #-}
-- | init a context where -- | init a context where
@ -38,35 +38,39 @@ init hashlen = unsafeDoIO (internalInit hashlen)
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: Int -- ^ algorithm hash size in bits hash :: (ByteArray digest, ByteArrayAccess ba)
-> ByteString -- ^ the data to hash => Int -- ^ algorithm hash size in bits
-> ByteString -- ^ the digest output -> ba -- ^ the data to hash
-> digest -- ^ the digest output
hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash hashlen d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt hashlen ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -- ^ algorithm hash size in bits hashlazy :: ByteArray digest
=> Int -- ^ algorithm hash size in bits
-> L.ByteString -- ^ the data to hash as a lazy bytestring -> L.ByteString -- ^ the data to hash as a lazy bytestring
-> ByteString -- ^ the digest output -> digest -- ^ the digest output
hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy hashlen l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt hashlen ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr

View File

@ -14,21 +14,21 @@ module Crypto.Hash.%%MODULENAME%%
( Ctx(..) ( Ctx(..)
-- * Incremental hashing Functions -- * Incremental hashing Functions
, init -- :: Ctx , init
, update -- :: Ctx -> ByteString -> Ctx , update
, updates -- :: Ctx -> [ByteString] -> Ctx , updates
, finalize -- :: Ctx -> ByteString , finalize
-- * Single Pass hashing -- * Single Pass hashing
, hash -- :: ByteString -> ByteString , hash
, hashlazy -- :: ByteString -> ByteString , hashlazy
) where ) where
import Prelude hiding (init) import Prelude hiding (init)
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import Crypto.Internal.Compat (unsafeDoIO) import Crypto.Internal.Compat (unsafeDoIO)
import Crypto.Hash.Internal.%%MODULENAME%% import Crypto.Hash.Internal.%%MODULENAME%%
{-# RULES "hash" forall b. finalize (update init b) = hash b #-} {-# RULES "hash" forall b. finalize (update init b) = hash b #-}
{-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-} {-# RULES "hash.list1" forall b. finalize (updates init [b]) = hash b #-}
@ -42,31 +42,37 @@ init = unsafeDoIO internalInit
{-# NOINLINE update #-} {-# NOINLINE update #-}
-- | update a context with a bytestring returning the new updated context -- | update a context with a bytestring returning the new updated context
update :: Ctx -- ^ the context to update update :: ByteArrayAccess ba
-> ByteString -- ^ the data to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> ba -- ^ the data to update with
-> Ctx -- ^ the updated context
update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d update ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> internalUpdate ptr d
{-# NOINLINE updates #-} {-# NOINLINE updates #-}
-- | updates a context with multiples bytestring returning the new updated context -- | updates a context with multiples bytestring returning the new updated context
updates :: Ctx -- ^ the context to update updates :: ByteArrayAccess ba
-> [ByteString] -- ^ a list of data bytestring to update with => Ctx -- ^ the context to update
-> Ctx -- ^ the updated context -> [ba] -- ^ a list of data bytestring to update with
-> Ctx -- ^ the updated context
updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d updates ctx d = unsafeDoIO $ withCtxCopy ctx $ \ptr -> mapM_ (internalUpdate ptr) d
{-# NOINLINE finalize #-} {-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring -- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString finalize :: ByteArray digest => Ctx -> digest
finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize finalize ctx = unsafeDoIO $ withCtxThrow ctx internalFinalize
{-# NOINLINE hash #-} {-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring -- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString hash :: (ByteArray digest, ByteArrayAccess ba)
=> ba
-> digest
hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hash d = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr internalInitAt ptr >> internalUpdate ptr d >> internalFinalize ptr
{-# NOINLINE hashlazy #-} {-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring -- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString hashlazy :: ByteArray digest
=> L.ByteString
-> digest
hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do hashlazy l = unsafeDoIO $ withCtxNewThrow $ \ptr -> do
internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr internalInitAt ptr >> mapM_ (internalUpdate ptr) (L.toChunks l) >> internalFinalize ptr