Browse files

experiments with phantom types and single digest type.

  • Loading branch information...
1 parent 10c6dc5 commit dc1e2f476e3966ffa7ee1dfd1bb8c6adcb31fb79 @vincenthz committed Nov 22, 2012
View
40 Bench/Bench.hs
@@ -17,28 +17,30 @@ import qualified Crypto.Hash.Tiger as Tiger
import qualified Crypto.Hash.Skein256 as Skein256
import qualified Crypto.Hash.Skein512 as Skein512
import qualified Crypto.Hash.Whirlpool as Whirlpool
+import Crypto.Hash
-hashmany (i,u,f) = f . foldl u i
+hashone f = digestToByteString . f
+hashmany (i,u,f) = digestToByteString . f . foldl u i
allHashs =
- [ ("MD2",MD2.hash, hashmany (MD2.init,MD2.update,MD2.finalize))
- , ("MD4",MD4.hash, hashmany (MD4.init,MD4.update,MD4.finalize))
- , ("MD5",MD5.hash, hashmany (MD5.init,MD5.update,MD5.finalize))
- , ("SHA1",SHA1.hash, hashmany (SHA1.init,SHA1.update,SHA1.finalize))
- , ("SHA2-224",SHA224.hash, hashmany (SHA224.init,SHA224.update,SHA224.finalize))
- , ("SHA2-256",SHA256.hash, hashmany (SHA256.init,SHA256.update,SHA256.finalize))
- , ("SHA2-384",SHA384.hash, hashmany (SHA384.init,SHA384.update,SHA384.finalize))
- , ("SHA2-512",SHA512.hash, hashmany (SHA512.init,SHA512.update,SHA512.finalize))
- , ("SHA2-512t-512",SHA512t.hash 512, hashmany (SHA512t.init 512,SHA512t.update,SHA512t.finalize))
- , ("SHA3-224",SHA3.hash 224, hashmany (SHA3.init 224,SHA3.update,SHA3.finalize))
- , ("SHA3-256",SHA3.hash 256, hashmany (SHA3.init 256,SHA3.update,SHA3.finalize))
- , ("SHA3-384",SHA3.hash 384, hashmany (SHA3.init 384,SHA3.update,SHA3.finalize))
- , ("SHA3-512",SHA3.hash 512, hashmany (SHA3.init 512,SHA3.update,SHA3.finalize))
- , ("RIPEMD160",RIPEMD160.hash, hashmany (RIPEMD160.init,RIPEMD160.update,RIPEMD160.finalize))
- , ("Tiger",Tiger.hash, hashmany (Tiger.init,Tiger.update,Tiger.finalize))
- , ("Skein256-256",Skein256.hash 256, hashmany (Skein256.init 256,Skein256.update,Skein256.finalize))
- , ("Skein512-512",Skein512.hash 512, hashmany (Skein512.init 512,Skein512.update,Skein512.finalize))
- , ("Whirlpool",Whirlpool.hash, hashmany (Whirlpool.init,Whirlpool.update,Whirlpool.finalize))
+ [ ("MD2",hashone MD2.hash, hashmany (MD2.init,MD2.update,MD2.finalize))
+ , ("MD4",hashone MD4.hash, hashmany (MD4.init,MD4.update,MD4.finalize))
+ , ("MD5",hashone MD5.hash, hashmany (MD5.init,MD5.update,MD5.finalize))
+ , ("SHA1",hashone SHA1.hash, hashmany (SHA1.init,SHA1.update,SHA1.finalize))
+ , ("SHA2-224",hashone SHA224.hash, hashmany (SHA224.init,SHA224.update,SHA224.finalize))
+ , ("SHA2-256",hashone SHA256.hash, hashmany (SHA256.init,SHA256.update,SHA256.finalize))
+ , ("SHA2-384",hashone SHA384.hash, hashmany (SHA384.init,SHA384.update,SHA384.finalize))
+ , ("SHA2-512",hashone SHA512.hash, hashmany (SHA512.init,SHA512.update,SHA512.finalize))
+ , ("SHA2-512t-512",hashone (SHA512t.hash 512), hashmany (SHA512t.init 512,SHA512t.update,SHA512t.finalize))
+ , ("SHA3-224",hashone (SHA3.hash 224), hashmany (SHA3.init 224,SHA3.update,SHA3.finalize))
+ , ("SHA3-256",hashone (SHA3.hash 256), hashmany (SHA3.init 256,SHA3.update,SHA3.finalize))
+ , ("SHA3-384",hashone (SHA3.hash 384), hashmany (SHA3.init 384,SHA3.update,SHA3.finalize))
+ , ("SHA3-512",hashone (SHA3.hash 512), hashmany (SHA3.init 512,SHA3.update,SHA3.finalize))
+ , ("RIPEMD160",hashone RIPEMD160.hash, hashmany (RIPEMD160.init,RIPEMD160.update,RIPEMD160.finalize))
+ , ("Tiger",hashone Tiger.hash, hashmany (Tiger.init,Tiger.update,Tiger.finalize))
+ , ("Skein256-256",hashone (Skein256.hash 256), hashmany (Skein256.init 256,Skein256.update,Skein256.finalize))
+ , ("Skein512-512",hashone (Skein512.hash 512), hashmany (Skein512.init 512,Skein512.update,Skein512.finalize))
+ , ("Whirlpool",hashone Whirlpool.hash, hashmany (Whirlpool.init,Whirlpool.update,Whirlpool.finalize))
]
benchHash :: a -> (a -> B.ByteString) -> Pure
View
8 Crypto/Hash.hs
@@ -0,0 +1,8 @@
+module Crypto.Hash
+ ( Digest
+ , digestToByteString
+ , digestToHexS
+ , digestToHex
+ ) where
+
+import Crypto.Hash.Types
View
45 Crypto/Hash/MD2.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.MD2
@@ -16,11 +16,11 @@ module Crypto.Hash.MD2
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest MD2
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest MD2
+ , hashlazy -- :: ByteString -> Digest MD2
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx MD2 where
- outputLength = Tagged (16 * 8)
- blockLength = Tagged (16 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize MD2 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data MD2 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data MD2
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md2_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest MD2)
finalizeInternalIO ptr =
- create digestSize (c_md2_finalize ptr)
+ Digest `fmap` create digestSize (c_md2_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest MD2
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest MD2
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md2_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest MD2
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md2_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/MD4.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.MD4
@@ -16,11 +16,11 @@ module Crypto.Hash.MD4
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest MD4
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest MD4
+ , hashlazy -- :: ByteString -> Digest MD4
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx MD4 where
- outputLength = Tagged (16 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize MD4 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data MD4 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data MD4
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md4_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest MD4)
finalizeInternalIO ptr =
- create digestSize (c_md4_finalize ptr)
+ Digest `fmap` create digestSize (c_md4_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest MD4
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest MD4
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md4_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest MD4
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md4_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/MD5.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.MD5
@@ -16,11 +16,11 @@ module Crypto.Hash.MD5
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest MD5
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest MD5
+ , hashlazy -- :: ByteString -> Digest MD5
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx MD5 where
- outputLength = Tagged (16 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize MD5 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data MD5 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data MD5
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_md5_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest MD5)
finalizeInternalIO ptr =
- create digestSize (c_md5_finalize ptr)
+ Digest `fmap` create digestSize (c_md5_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest MD5
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest MD5
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md5_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest MD5
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md5_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/RIPEMD160.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.RIPEMD160
@@ -16,11 +16,11 @@ module Crypto.Hash.RIPEMD160
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest RIPEMD160
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest RIPEMD160
+ , hashlazy -- :: ByteString -> Digest RIPEMD160
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx RIPEMD160 where
- outputLength = Tagged (20 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize RIPEMD160 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data RIPEMD160 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data RIPEMD160
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_ripemd160_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest RIPEMD160)
finalizeInternalIO ptr =
- create digestSize (c_ripemd160_finalize ptr)
+ Digest `fmap` create digestSize (c_ripemd160_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest RIPEMD160
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest RIPEMD160
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_ripemd160_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest RIPEMD160
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_ripemd160_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/SHA1.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA1
@@ -16,11 +16,11 @@ module Crypto.Hash.SHA1
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA1
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest SHA1
+ , hashlazy -- :: ByteString -> Digest SHA1
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx SHA1 where
- outputLength = Tagged (20 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize SHA1 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data SHA1 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data SHA1
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha1_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA1)
finalizeInternalIO ptr =
- create digestSize (c_sha1_finalize ptr)
+ Digest `fmap` create digestSize (c_sha1_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest SHA1
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest SHA1
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha1_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest SHA1
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha1_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/SHA224.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA224
@@ -16,11 +16,11 @@ module Crypto.Hash.SHA224
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA224
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest SHA224
+ , hashlazy -- :: ByteString -> Digest SHA224
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx SHA224 where
- outputLength = Tagged (28 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize SHA224 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data SHA224 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data SHA224
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha224_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA224)
finalizeInternalIO ptr =
- create digestSize (c_sha224_finalize ptr)
+ Digest `fmap` create digestSize (c_sha224_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest SHA224
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest SHA224
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha224_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest SHA224
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha224_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/SHA256.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA256
@@ -16,11 +16,11 @@ module Crypto.Hash.SHA256
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA256
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest SHA256
+ , hashlazy -- :: ByteString -> Digest SHA256
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx SHA256 where
- outputLength = Tagged (32 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize SHA256 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data SHA256 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data SHA256
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha256_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA256)
finalizeInternalIO ptr =
- create digestSize (c_sha256_finalize ptr)
+ Digest `fmap` create digestSize (c_sha256_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest SHA256
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest SHA256
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha256_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest SHA256
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha256_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
20 Crypto/Hash/SHA3.hs
@@ -1,5 +1,5 @@
{-# LANGUAGE ForeignFunctionInterface #-}
-
+{-# LANGUAGE EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA3
-- License : BSD-style
@@ -15,11 +15,11 @@ module Crypto.Hash.SHA3
-- * Incremental hashing Functions
, init -- :: Int -> Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA3
-- * Single Pass hashing
- , hash -- :: Int -> ByteString -> ByteString
- , hashlazy -- :: Int -> ByteString -> ByteString
+ , hash -- :: Int -> ByteString -> Digest SHA3
+ , hashlazy -- :: Int -> ByteString -> Digest SHA3
) where
import Prelude hiding (init)
@@ -32,8 +32,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
+import Crypto.Hash.Types
data Ctx = Ctx !ByteString
+data SHA3
{-# INLINE sizeCtx #-}
sizeCtx :: Int
@@ -89,9 +91,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha3_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA3)
finalizeInternalIO ptr =
- peekHashlen ptr >>= \digestSize -> create digestSize (c_sha3_finalize ptr)
+ peekHashlen ptr >>= \digestSize -> (Digest `fmap` create digestSize (c_sha3_finalize ptr))
{-# NOINLINE init #-}
-- | init a context
@@ -105,17 +107,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest SHA3
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: Int -> ByteString -> ByteString
+hash :: Int -> ByteString -> Digest SHA3
hash hashlen d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha3_init ptr (fromIntegral hashlen) >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: Int -> L.ByteString -> ByteString
+hashlazy :: Int -> L.ByteString -> Digest SHA3
hashlazy hashlen l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha3_init ptr (fromIntegral hashlen) >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/SHA384.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA384
@@ -16,11 +16,11 @@ module Crypto.Hash.SHA384
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA384
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest SHA384
+ , hashlazy -- :: ByteString -> Digest SHA384
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx SHA384 where
- outputLength = Tagged (48 * 8)
- blockLength = Tagged (128 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize SHA384 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data SHA384 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data SHA384
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha384_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA384)
finalizeInternalIO ptr =
- create digestSize (c_sha384_finalize ptr)
+ Digest `fmap` create digestSize (c_sha384_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest SHA384
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest SHA384
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha384_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest SHA384
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha384_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
61 Crypto/Hash/SHA512.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA512
@@ -17,51 +17,27 @@ module Crypto.Hash.SHA512
, init -- :: Ctx
, init_t -- :: Int -> Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest SHA512
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest SHA512
+ , hashlazy -- :: ByteString -> Digest SHA512
) where
import Prelude hiding (init)
-import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
-import Data.ByteString.Internal (create, toForeignPtr)
+import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx SHA512 where
- outputLength = Tagged (64 * 8)
- blockLength = Tagged (128 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize SHA512 where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data SHA512 = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data SHA512
{-# INLINE digestSize #-}
digestSize :: Int
@@ -118,38 +94,37 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_sha512_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest SHA512)
finalizeInternalIO ptr =
- create digestSize (c_sha512_finalize ptr)
+ Digest `fmap` create digestSize (c_sha512_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
-init = unsafePerformIO $ withCtxNew $ c_sha512_init
+init = inlinePerformIO $ withCtxNew $ c_sha512_init
-{-# NOINLINE init_t #-}
-- | init a context using FIPS 180-4 for truncated SHA512
init_t :: Int -> Ctx
-init_t t = unsafePerformIO $ withCtxNew $ \ptr -> c_sha512_init_t ptr t
+init_t t = inlinePerformIO $ withCtxNew $ \ptr -> c_sha512_init_t ptr t
{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
-update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
+update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
-finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
+finalize :: Ctx -> Digest SHA512
+finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
-hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
+hash :: ByteString -> Digest SHA512
+hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha512_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
-hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
+hashlazy :: L.ByteString -> Digest SHA512
+hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha512_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
16 Crypto/Hash/SHA512t.hs
@@ -1,5 +1,4 @@
-{-# LANGUAGE MultiParamTypeClasses #-}
-
+{-# LANGUAGE EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.SHA512t
-- License : BSD-style
@@ -11,7 +10,7 @@
--
module Crypto.Hash.SHA512t
( Ctx(..)
- --, SHA512t
+ , SHA512t
-- * Incremental hashing Functions
, init -- :: Ctx
@@ -30,8 +29,10 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Crypto.Hash.SHA512 as SHA512
+import Crypto.Hash.Types
data Ctx = Ctx !Int !SHA512.Ctx
+data SHA512t
-- | init a context
init :: Int -> Ctx
@@ -42,13 +43,14 @@ update :: Ctx -> ByteString -> 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 :: Ctx -> Digest SHA512t
+finalize (Ctx sz ctx) = Digest $ B.take (sz `div` 8) digest
+ where (Digest digest) = SHA512.finalize ctx
-- | hash a strict bytestring into a digest bytestring
-hash :: Int -> ByteString -> ByteString
+hash :: Int -> ByteString -> Digest SHA512t
hash t = finalize . update (init t)
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: Int -> L.ByteString -> ByteString
+hashlazy :: Int -> L.ByteString -> Digest SHA512t
hashlazy t = finalize . foldl' update (init t) . L.toChunks
View
42 Crypto/Hash/Skein256.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.Skein256
@@ -19,8 +19,8 @@ module Crypto.Hash.Skein256
, finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- , hash -- :: Int -> ByteString -> ByteString
- , hashlazy -- :: Int -> ByteString -> ByteString
+ , hash -- :: Int -> ByteString -> Digest Skein256
+ , hashlazy -- :: Int -> ByteString -> Digest Skein256
) where
import Prelude hiding (init)
@@ -35,32 +35,10 @@ import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, inlinePerformIO, toForeignPtr)
import Data.Word
import Data.Bits
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx Skein256 where
- outputLength = Tagged (32 * 8)
- blockLength = Tagged (32 * 8)
- initialCtx = init (32 * 8)
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize Skein256 where
- get = liftM Digest (getByteString 32)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types
data Ctx = Ctx !ByteString
-data Skein256 = Digest !ByteString
- deriving (Eq,Ord,Show)
+data Skein256
sizeCtx :: Int
sizeCtx = 88
@@ -116,9 +94,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein256_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest Skein256)
finalizeInternalIO ptr =
- peekHashlen ptr >>= \digestSize -> create digestSize (c_skein256_finalize ptr)
+ peekHashlen ptr >>= \digestSize -> (Digest `fmap` create digestSize (c_skein256_finalize ptr))
{-# NOINLINE init #-}
-- | init a context
@@ -132,17 +110,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest Skein256
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: Int -> ByteString -> ByteString
+hash :: Int -> ByteString -> Digest Skein256
hash hashlen d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_skein256_init ptr (fromIntegral hashlen) >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: Int -> L.ByteString -> ByteString
+hashlazy :: Int -> L.ByteString -> Digest Skein256
hashlazy hashlen l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_skein256_init ptr (fromIntegral hashlen) >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
42 Crypto/Hash/Skein512.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.Skein512
@@ -19,8 +19,8 @@ module Crypto.Hash.Skein512
, finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- , hash -- :: Int -> ByteString -> ByteString
- , hashlazy -- :: Int -> ByteString -> ByteString
+ , hash -- :: Int -> ByteString -> Digest Skein512
+ , hashlazy -- :: Int -> ByteString -> Digest Skein512
) where
import Prelude hiding (init)
@@ -35,32 +35,10 @@ import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, inlinePerformIO, toForeignPtr)
import Data.Word
import Data.Bits
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx Skein512 where
- outputLength = Tagged (64 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init (64 * 8)
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize Skein512 where
- get = liftM Digest (getByteString 64)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types
data Ctx = Ctx !ByteString
-data Skein512 = Digest !ByteString
- deriving (Eq,Ord,Show)
+data Skein512
sizeCtx :: Int
sizeCtx = 152
@@ -116,9 +94,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_skein512_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest Skein512)
finalizeInternalIO ptr =
- peekHashlen ptr >>= \digestSize -> create digestSize (c_skein512_finalize ptr)
+ peekHashlen ptr >>= \digestSize -> (Digest `fmap` create digestSize (c_skein512_finalize ptr))
{-# NOINLINE init #-}
-- | init a context
@@ -132,17 +110,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest Skein512
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: Int -> ByteString -> ByteString
+hash :: Int -> ByteString -> Digest Skein512
hash hashlen d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_skein512_init ptr (fromIntegral hashlen) >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: Int -> L.ByteString -> ByteString
+hashlazy :: Int -> L.ByteString -> Digest Skein512
hashlazy hashlen l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_skein512_init ptr (fromIntegral hashlen) >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
45 Crypto/Hash/Tiger.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.Tiger
@@ -16,11 +16,11 @@ module Crypto.Hash.Tiger
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest Tiger
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest Tiger
+ , hashlazy -- :: ByteString -> Digest Tiger
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx Tiger where
- outputLength = Tagged (24 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize Tiger where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data Tiger = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data Tiger
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_tiger_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest Tiger)
finalizeInternalIO ptr =
- create digestSize (c_tiger_finalize ptr)
+ Digest `fmap` create digestSize (c_tiger_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest Tiger
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest Tiger
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_tiger_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest Tiger
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_tiger_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
76 Crypto/Hash/Types.hs
@@ -0,0 +1,76 @@
+{-# LANGUAGE MagicHash, BangPatterns #-}
+module Crypto.Hash.Types
+ ( Digest(..)
+ , digestToHex
+ , digestToHexS
+ ) where
+
+import Data.ByteString (ByteString)
+import qualified Data.ByteString.Internal as B
+import qualified Data.ByteString.Char8 as BC
+import GHC.Prim
+import GHC.Types
+import GHC.Word
+import Foreign.ForeignPtr (withForeignPtr)
+import Foreign.Ptr (plusPtr)
+import Foreign.Storable (poke, peek)
+
+-- newtype Ctx a = Ctx { contextToByteString :: ByteString }
+
+-- | Represent a digest.
+newtype Digest a = Digest { digestToByteString :: ByteString }
+ deriving (Eq,Ord)
+
+digestToHexS :: Digest a -> String
+digestToHexS = BC.unpack . digestToHex
+
+digestToHex :: Digest a -> ByteString
+digestToHex digest = B.unsafeCreate (len*2) $ \d ->
+ withForeignPtr fp $ \s -> start d (s `plusPtr` off)
+ where start db sb = loop db sb
+ where end = sb `plusPtr` len
+ loop d s
+ | d == end = return ()
+ | otherwise = do b <- fromIntegral `fmap` (peek s :: IO Word8)
+ poke d (tlookup tableHi b)
+ poke (d `plusPtr` 1) (tlookup tableLo b)
+ loop (d `plusPtr` 2) (s `plusPtr` 1)
+
+ tlookup :: Addr# -> Int -> Word8
+ tlookup table (I# index) = W8# (indexWord8OffAddr# table index)
+
+ !(B.PS fp off len) = digestToByteString digest
+ !tableLo =
+ "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\
+ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66"#
+ !tableHi =
+ "\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\
+ \\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\
+ \\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\
+ \\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\
+ \\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\
+ \\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\
+ \\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\
+ \\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\
+ \\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\
+ \\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\
+ \\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\
+ \\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\
+ \\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\
+ \\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\
+ \\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\
+ \\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66"#
View
45 Crypto/Hash/Whirlpool.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses, EmptyDataDecls #-}
-- |
-- Module : Crypto.Hash.Whirlpool
@@ -16,11 +16,11 @@ module Crypto.Hash.Whirlpool
-- * Incremental hashing Functions
, init -- :: Ctx
, update -- :: Ctx -> ByteString -> Ctx
- , finalize -- :: Ctx -> ByteString
+ , finalize -- :: Ctx -> Digest Whirlpool
-- * Single Pass hashing
- , hash -- :: ByteString -> ByteString
- , hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> Digest Whirlpool
+ , hashlazy -- :: ByteString -> Digest Whirlpool
) where
import Prelude hiding (init)
@@ -33,33 +33,10 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word
-
-#ifdef HAVE_CRYPTOAPI
-
-import Control.Monad (liftM)
-import Data.Serialize (Serialize(..))
-import Data.Serialize.Get (getByteString)
-import Data.Serialize.Put (putByteString)
-import Data.Tagged (Tagged(..))
-import qualified Crypto.Classes as C (Hash(..))
-
-instance C.Hash Ctx Whirlpool where
- outputLength = Tagged (64 * 8)
- blockLength = Tagged (64 * 8)
- initialCtx = init
- updateCtx = update
- finalize ctx bs = Digest . finalize $ update ctx bs
-
-instance Serialize Whirlpool where
- get = liftM Digest (getByteString digestSize)
- put (Digest d) = putByteString d
-
-#endif
+import Crypto.Hash.Types (Digest(..))
data Ctx = Ctx !ByteString
-data Whirlpool = Digest !ByteString
- deriving (Eq,Ord,Show)
-
+data Whirlpool
{-# INLINE digestSize #-}
digestSize :: Int
@@ -113,9 +90,9 @@ updateInternalIO :: Ptr Ctx -> ByteString -> IO ()
updateInternalIO ptr d =
unsafeUseAsCStringLen d (\(cs, len) -> c_whirlpool_update ptr (castPtr cs) (fromIntegral len))
-finalizeInternalIO :: Ptr Ctx -> IO ByteString
+finalizeInternalIO :: Ptr Ctx -> IO (Digest Whirlpool)
finalizeInternalIO ptr =
- create digestSize (c_whirlpool_finalize ptr)
+ Digest `fmap` create digestSize (c_whirlpool_finalize ptr)
{-# NOINLINE init #-}
-- | init a context
@@ -129,17 +106,17 @@ update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr
{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
-finalize :: Ctx -> ByteString
+finalize :: Ctx -> Digest Whirlpool
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO
{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
-hash :: ByteString -> ByteString
+hash :: ByteString -> Digest Whirlpool
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_whirlpool_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr
{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
-hashlazy :: L.ByteString -> ByteString
+hashlazy :: L.ByteString -> Digest Whirlpool
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_whirlpool_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
View
68 Tests/KAT.hs
@@ -18,41 +18,63 @@ import qualified Crypto.Hash.Tiger as Tiger
import qualified Crypto.Hash.Skein256 as Skein256
import qualified Crypto.Hash.Skein512 as Skein512
import qualified Crypto.Hash.Whirlpool as Whirlpool
+import Crypto.Hash
v0 = ""
v1 = "The quick brown fox jumps over the lazy dog"
v2 = "The quick brown fox jumps over the lazy cog"
vectors = [ v0, v1, v2 ]
data HashFct = HashFct
- { fctHash :: (B.ByteString -> B.ByteString)
- , fctInc :: ([B.ByteString] -> B.ByteString) }
+ { fctHash :: (B.ByteString -> String)
+ , fctInc :: ([B.ByteString] -> String) }
-hashinc i u f = f . foldl u i
+hashf :: (B.ByteString -> Digest b) -> B.ByteString -> String
+hashf f = digestToHexS . f
-md2Hash = HashFct { fctHash = MD2.hash, fctInc = hashinc MD2.init MD2.update MD2.finalize }
-md4Hash = HashFct { fctHash = MD4.hash, fctInc = hashinc MD4.init MD4.update MD4.finalize }
-md5Hash = HashFct { fctHash = MD5.hash, fctInc = hashinc MD5.init MD5.update MD5.finalize }
+hashinc :: a -> (a -> B.ByteString -> a) -> (a -> Digest b) -> [B.ByteString] -> String
+hashinc i u f = digestToHexS . f . foldl u i
-sha1Hash = HashFct { fctHash = SHA1.hash, fctInc = hashinc SHA1.init SHA1.update SHA1.finalize }
+md2Hash = HashFct { fctHash = hashf MD2.hash
+ , fctInc = hashinc MD2.init MD2.update MD2.finalize }
+md4Hash = HashFct { fctHash = hashf MD4.hash
+ , fctInc = hashinc MD4.init MD4.update MD4.finalize }
+md5Hash = HashFct { fctHash = hashf MD5.hash
+ , fctInc = hashinc MD5.init MD5.update MD5.finalize }
-sha224Hash = HashFct { fctHash = SHA224.hash, fctInc = hashinc SHA224.init SHA224.update SHA224.finalize }
-sha256Hash = HashFct { fctHash = SHA256.hash, fctInc = hashinc SHA256.init SHA256.update SHA256.finalize }
+sha1Hash = HashFct { fctHash = hashf SHA1.hash
+ , fctInc = hashinc SHA1.init SHA1.update SHA1.finalize }
-sha384Hash = HashFct { fctHash = SHA384.hash, fctInc = hashinc SHA384.init SHA384.update SHA384.finalize }
-sha512Hash = HashFct { fctHash = SHA512.hash, fctInc = hashinc SHA512.init SHA512.update SHA512.finalize }
-sha512_224Hash = HashFct { fctHash = SHA512t.hash 224, fctInc = hashinc (SHA512t.init 224) SHA512t.update SHA512t.finalize }
-sha512_256Hash = HashFct { fctHash = SHA512t.hash 256, fctInc = hashinc (SHA512t.init 256) SHA512t.update SHA512t.finalize }
+sha224Hash = HashFct { fctHash = hashf SHA224.hash
+ , fctInc = hashinc SHA224.init SHA224.update SHA224.finalize }
+sha256Hash = HashFct { fctHash = digestToHexS . SHA256.hash
+ , fctInc = hashinc SHA256.init SHA256.update SHA256.finalize }
-sha3Hash i = HashFct { fctHash = SHA3.hash i, fctInc = hashinc (SHA3.init i) SHA3.update SHA3.finalize }
+sha384Hash = HashFct { fctHash = hashf SHA384.hash
+ , fctInc = hashinc SHA384.init SHA384.update SHA384.finalize }
+sha512Hash = HashFct { fctHash = hashf SHA512.hash
+ , fctInc = hashinc SHA512.init SHA512.update SHA512.finalize }
-ripemd160Hash = HashFct { fctHash = RIPEMD160.hash, fctInc = hashinc RIPEMD160.init RIPEMD160.update RIPEMD160.finalize }
-tigerHash = HashFct { fctHash = Tiger.hash, fctInc = hashinc Tiger.init Tiger.update Tiger.finalize }
+sha512_224Hash = HashFct { fctHash = hashf (SHA512t.hash 224)
+ , fctInc = hashinc (SHA512t.init 224) SHA512t.update SHA512t.finalize }
+sha512_256Hash = HashFct { fctHash = hashf (SHA512t.hash 256)
+ , fctInc = hashinc (SHA512t.init 256) SHA512t.update SHA512t.finalize }
-skein256Hash x = HashFct { fctHash = Skein256.hash x, fctInc = hashinc (Skein256.init x) Skein256.update Skein256.finalize }
-skein512Hash x = HashFct { fctHash = Skein512.hash x, fctInc = hashinc (Skein512.init x) Skein512.update Skein512.finalize }
+sha3Hash i = HashFct { fctHash = hashf (SHA3.hash i)
+ , fctInc = hashinc (SHA3.init i) SHA3.update SHA3.finalize }
-whirlpoolHash = HashFct { fctHash = Whirlpool.hash, fctInc = hashinc Whirlpool.init Whirlpool.update Whirlpool.finalize }
+ripemd160Hash = HashFct { fctHash = hashf RIPEMD160.hash
+ , fctInc = hashinc RIPEMD160.init RIPEMD160.update RIPEMD160.finalize }
+tigerHash = HashFct { fctHash = hashf Tiger.hash
+ , fctInc = hashinc Tiger.init Tiger.update Tiger.finalize }
+
+skein256Hash x = HashFct { fctHash = hashf (Skein256.hash x)
+ , fctInc = hashinc (Skein256.init x) Skein256.update Skein256.finalize }
+skein512Hash x = HashFct { fctHash = hashf (Skein512.hash x)
+ , fctInc = hashinc (Skein512.init x) Skein512.update Skein512.finalize }
+
+whirlpoolHash = HashFct { fctHash = hashf Whirlpool.hash
+ , fctInc = hashinc Whirlpool.init Whirlpool.update Whirlpool.finalize }
results :: [ (String, HashFct, [String]) ]
results = [
@@ -151,6 +173,7 @@ results = [
"10f8caabb5b179861da5e447d34b84d604e3eb81830880e1c2135ffc94580a47cb21f6243ec0053d58b1124d13af2090033659075ee718e0f111bb3f69fb24cf" ])
]
+{-
hexalise s = concatMap (\c -> [ hex $ c `div` 16, hex $ c `mod` 16 ]) s
where hex i
| i >= 0 && i <= 9 = fromIntegral (ord '0') + i
@@ -159,6 +182,7 @@ hexalise s = concatMap (\c -> [ hex $ c `div` 16, hex $ c `mod` 16 ]) s
hexaliseB :: B.ByteString -> B.ByteString
hexaliseB = B.pack . hexalise . B.unpack
+-}
splitB l b =
if B.length b > l
@@ -168,11 +192,13 @@ splitB l b =
else
[ b ]
+{-
showHash :: B.ByteString -> String
showHash = map (toEnum.fromEnum) . hexalise . B.unpack
+-}
-runhash hash v = showHash $ (fctHash hash) $ v
-runhashinc hash v = showHash $ (fctInc hash) $ v
+runhash hash v = (fctHash hash) $ v
+runhashinc hash v = (fctInc hash) $ v
makeTestAlg (name, hash, results) = concatMap maketest $ zip3 [0..] vectors results
where
View
13 cryptohash.cabal
@@ -28,19 +28,13 @@ Flag benchmark
Description: Build benchmark test
Default: False
-Flag cryptoapi
- Description: Defines crypto-api instances
- Default: True
-
Library
- Build-Depends: base >= 4 && < 6, bytestring
- if flag(cryptoapi)
- Build-depends: crypto-api >= 0.5, tagged >= 0.1, cereal >= 0.2
- cpp-options: -DHAVE_CRYPTOAPI
+ Build-Depends: base >= 4 && < 6, bytestring, ghc-prim
if impl(ghc >= 7.2.1)
Extensions: Trustworthy
Extensions: ForeignFunctionInterface
- Exposed-modules: Crypto.Hash.SHA1
+ Exposed-modules: Crypto.Hash
+ Crypto.Hash.SHA1
Crypto.Hash.SHA224
Crypto.Hash.SHA256
Crypto.Hash.SHA384
@@ -56,6 +50,7 @@ Library
Crypto.Hash.Tiger
Crypto.Hash.Whirlpool
Crypto.MAC.HMAC
+ Other-modules: Crypto.Hash.Types
ghc-options: -Wall -O2 -optc-O3 -fno-cse -fwarn-tabs
C-sources: cbits/sha1.c
cbits/sha256.c

0 comments on commit dc1e2f4

Please sign in to comment.