diff --git a/Crypto/Hash/Internal/Kekkak.hs b/Crypto/Hash/Internal/Kekkak.hs index e258695..1721891 100644 --- a/Crypto/Hash/Internal/Kekkak.hs +++ b/Crypto/Hash/Internal/Kekkak.hs @@ -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) diff --git a/Crypto/Hash/Internal/MD2.hs b/Crypto/Hash/Internal/MD2.hs index da83c50..0684c21 100644 --- a/Crypto/Hash/Internal/MD2.hs +++ b/Crypto/Hash/Internal/MD2.hs @@ -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) diff --git a/Crypto/Hash/Internal/MD4.hs b/Crypto/Hash/Internal/MD4.hs index 864a450..0f3939e 100644 --- a/Crypto/Hash/Internal/MD4.hs +++ b/Crypto/Hash/Internal/MD4.hs @@ -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) diff --git a/Crypto/Hash/Internal/MD5.hs b/Crypto/Hash/Internal/MD5.hs index 37abf08..b5cb240 100644 --- a/Crypto/Hash/Internal/MD5.hs +++ b/Crypto/Hash/Internal/MD5.hs @@ -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) diff --git a/Crypto/Hash/Internal/RIPEMD160.hs b/Crypto/Hash/Internal/RIPEMD160.hs index 5420aff..12c21d5 100644 --- a/Crypto/Hash/Internal/RIPEMD160.hs +++ b/Crypto/Hash/Internal/RIPEMD160.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA1.hs b/Crypto/Hash/Internal/SHA1.hs index 9689299..1cc2aac 100644 --- a/Crypto/Hash/Internal/SHA1.hs +++ b/Crypto/Hash/Internal/SHA1.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA224.hs b/Crypto/Hash/Internal/SHA224.hs index 8c0cb45..9d7ff35 100644 --- a/Crypto/Hash/Internal/SHA224.hs +++ b/Crypto/Hash/Internal/SHA224.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA256.hs b/Crypto/Hash/Internal/SHA256.hs index 48ea915..d47d6f4 100644 --- a/Crypto/Hash/Internal/SHA256.hs +++ b/Crypto/Hash/Internal/SHA256.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA3.hs b/Crypto/Hash/Internal/SHA3.hs index cb4c9f1..f3c652b 100644 --- a/Crypto/Hash/Internal/SHA3.hs +++ b/Crypto/Hash/Internal/SHA3.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA384.hs b/Crypto/Hash/Internal/SHA384.hs index 466b8e0..729e36e 100644 --- a/Crypto/Hash/Internal/SHA384.hs +++ b/Crypto/Hash/Internal/SHA384.hs @@ -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) diff --git a/Crypto/Hash/Internal/SHA512.hs b/Crypto/Hash/Internal/SHA512.hs index e9e79ab..d654995 100644 --- a/Crypto/Hash/Internal/SHA512.hs +++ b/Crypto/Hash/Internal/SHA512.hs @@ -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) diff --git a/Crypto/Hash/Internal/Skein256.hs b/Crypto/Hash/Internal/Skein256.hs index c854185..e651bad 100644 --- a/Crypto/Hash/Internal/Skein256.hs +++ b/Crypto/Hash/Internal/Skein256.hs @@ -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) diff --git a/Crypto/Hash/Internal/Skein512.hs b/Crypto/Hash/Internal/Skein512.hs index 03990ae..b14b94a 100644 --- a/Crypto/Hash/Internal/Skein512.hs +++ b/Crypto/Hash/Internal/Skein512.hs @@ -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) diff --git a/Crypto/Hash/Internal/Tiger.hs b/Crypto/Hash/Internal/Tiger.hs index 8e89787..663ffcf 100644 --- a/Crypto/Hash/Internal/Tiger.hs +++ b/Crypto/Hash/Internal/Tiger.hs @@ -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) diff --git a/Crypto/Hash/Internal/Whirlpool.hs b/Crypto/Hash/Internal/Whirlpool.hs index 63daffd..3c21dda 100644 --- a/Crypto/Hash/Internal/Whirlpool.hs +++ b/Crypto/Hash/Internal/Whirlpool.hs @@ -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) diff --git a/Crypto/Hash/Kekkak.hs b/Crypto/Hash/Kekkak.hs index 47d821c..d08952a 100644 --- a/Crypto/Hash/Kekkak.hs +++ b/Crypto/Hash/Kekkak.hs @@ -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 diff --git a/Crypto/Hash/MD2.hs b/Crypto/Hash/MD2.hs index 66bfa2d..750a0e8 100644 --- a/Crypto/Hash/MD2.hs +++ b/Crypto/Hash/MD2.hs @@ -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 diff --git a/Crypto/Hash/MD4.hs b/Crypto/Hash/MD4.hs index 357f214..a307f8e 100644 --- a/Crypto/Hash/MD4.hs +++ b/Crypto/Hash/MD4.hs @@ -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 diff --git a/Crypto/Hash/MD5.hs b/Crypto/Hash/MD5.hs index 4b980a7..432cc19 100644 --- a/Crypto/Hash/MD5.hs +++ b/Crypto/Hash/MD5.hs @@ -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 diff --git a/Crypto/Hash/RIPEMD160.hs b/Crypto/Hash/RIPEMD160.hs index 4753d0f..fbb3c41 100644 --- a/Crypto/Hash/RIPEMD160.hs +++ b/Crypto/Hash/RIPEMD160.hs @@ -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 diff --git a/Crypto/Hash/SHA1.hs b/Crypto/Hash/SHA1.hs index 2598825..199b3b0 100644 --- a/Crypto/Hash/SHA1.hs +++ b/Crypto/Hash/SHA1.hs @@ -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 diff --git a/Crypto/Hash/SHA224.hs b/Crypto/Hash/SHA224.hs index 2e400aa..a015f46 100644 --- a/Crypto/Hash/SHA224.hs +++ b/Crypto/Hash/SHA224.hs @@ -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 diff --git a/Crypto/Hash/SHA256.hs b/Crypto/Hash/SHA256.hs index 16c4b1a..4c74dbd 100644 --- a/Crypto/Hash/SHA256.hs +++ b/Crypto/Hash/SHA256.hs @@ -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 diff --git a/Crypto/Hash/SHA3.hs b/Crypto/Hash/SHA3.hs index a295268..0aea5f2 100644 --- a/Crypto/Hash/SHA3.hs +++ b/Crypto/Hash/SHA3.hs @@ -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 diff --git a/Crypto/Hash/SHA384.hs b/Crypto/Hash/SHA384.hs index 5095536..9f7dad5 100644 --- a/Crypto/Hash/SHA384.hs +++ b/Crypto/Hash/SHA384.hs @@ -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 diff --git a/Crypto/Hash/SHA512.hs b/Crypto/Hash/SHA512.hs index 19456a3..94f02b0 100644 --- a/Crypto/Hash/SHA512.hs +++ b/Crypto/Hash/SHA512.hs @@ -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 diff --git a/Crypto/Hash/SHA512t.hs b/Crypto/Hash/SHA512t.hs index 8337556..80c1c71 100644 --- a/Crypto/Hash/SHA512t.hs +++ b/Crypto/Hash/SHA512t.hs @@ -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 diff --git a/Crypto/Hash/Skein256.hs b/Crypto/Hash/Skein256.hs index fb50f01..09d8d35 100644 --- a/Crypto/Hash/Skein256.hs +++ b/Crypto/Hash/Skein256.hs @@ -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 diff --git a/Crypto/Hash/Skein512.hs b/Crypto/Hash/Skein512.hs index 55dd1bc..d9c217f 100644 --- a/Crypto/Hash/Skein512.hs +++ b/Crypto/Hash/Skein512.hs @@ -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 diff --git a/Crypto/Hash/Tiger.hs b/Crypto/Hash/Tiger.hs index 3412229..af29922 100644 --- a/Crypto/Hash/Tiger.hs +++ b/Crypto/Hash/Tiger.hs @@ -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 diff --git a/Crypto/Hash/Whirlpool.hs b/Crypto/Hash/Whirlpool.hs index 6fe60c4..73cc761 100644 --- a/Crypto/Hash/Whirlpool.hs +++ b/Crypto/Hash/Whirlpool.hs @@ -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 diff --git a/gen/template/hash-internal-len.hs b/gen/template/hash-internal-len.hs index 10503c3..c0663b2 100644 --- a/gen/template/hash-internal-len.hs +++ b/gen/template/hash-internal-len.hs @@ -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) diff --git a/gen/template/hash-internal.hs b/gen/template/hash-internal.hs index 0d95a9c..bf1c270 100644 --- a/gen/template/hash-internal.hs +++ b/gen/template/hash-internal.hs @@ -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) diff --git a/gen/template/hash-len.hs b/gen/template/hash-len.hs index 6785e66..7379b79 100644 --- a/gen/template/hash-len.hs +++ b/gen/template/hash-len.hs @@ -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 diff --git a/gen/template/hash.hs b/gen/template/hash.hs index e21604a..8b58c12 100644 --- a/gen/template/hash.hs +++ b/gen/template/hash.hs @@ -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