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
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
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
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_md2_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md2_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.MD4
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_md4_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md4_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.MD5
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_md5_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md5_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.RIPEMD160
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 128 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_ripemd160_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_ripemd160_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA1
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_sha1_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha1_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA224
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_sha224_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha224_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.SHA256
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_sha256_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha256_finalize ptr)

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.SHA3
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
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
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_sha384_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha384_finalize ptr)

View File

@ -20,18 +20,17 @@ module Crypto.Hash.Internal.SHA512
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxNew
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
, withCtxNew
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -75,18 +74,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_sha512_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha512_finalize ptr)

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.Skein256
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 96 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
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
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 160 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
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
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_tiger_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_tiger_finalize ptr)

View File

@ -25,12 +25,11 @@ module Crypto.Hash.Internal.Whirlpool
, withCtxThrow
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 168 internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_whirlpool_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_whirlpool_finalize ptr)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,17 +20,15 @@ module Crypto.Hash.SHA512t
, hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
import Data.List (foldl')
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Prelude hiding (init, take)
import Data.List (foldl')
import qualified Data.ByteString.Lazy as L
import qualified Crypto.Hash.SHA512 as SHA512
import Crypto.Internal.Compat
--import qualified Crypto.Hash.Internal.SHA512 as SHA512
import Crypto.Internal.Compat
import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, take)
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
data Ctx = Ctx !Int !SHA512.Ctx
@ -40,17 +38,17 @@ init :: Int -> Ctx
init t = Ctx t $ unsafeDoIO $ withCtxNew $ \ptr -> SHA512t.internalInitAt t ptr
-- | 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)
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize (Ctx sz ctx) = B.take (sz `div` 8) (SHA512.finalize ctx)
finalize :: ByteArray digest => Ctx -> digest
finalize (Ctx sz ctx) = take (sz `div` 8) (SHA512.finalize ctx)
-- | 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 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

View File

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

View File

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

View File

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

View File

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

View File

@ -24,13 +24,12 @@ module Crypto.Hash.Internal.%%MODULENAME%%
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -73,19 +72,19 @@ internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc %%SIZECTX%% (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
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
) where
import Foreign.Ptr
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create)
import Data.Word
import Crypto.Internal.Memory
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
@ -71,18 +70,18 @@ internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc %%SIZECTX%% internalInitAt
-- | Update a context in place
internalUpdate :: Ptr Ctx -> ByteString -> IO ()
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
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.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- 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 =
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
internalFinalize :: Ptr Ctx -> IO ByteString
internalFinalize ptr = create digestSize (c_%%HASHNAME%%_finalize ptr)
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_%%HASHNAME%%_finalize ptr)

View File

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

View File

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