Permalink
Browse files

add definition for crypto-api and depends on it optionally for now.

  • Loading branch information...
1 parent 59f6a59 commit 3d6bc5a62001241fe7275ba04b91b8a0a2f84425 @vincenthz committed Oct 24, 2010
View
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
-- |
-- Module : Crypto.Hash.MD2
@@ -9,17 +9,18 @@
--
-- A module containing MD2 bindings
--
-module Crypto.Hash.MD2 (
- Ctx(..),
+module Crypto.Hash.MD2
+ ( Ctx(..)
+ , MD2
-- * Incremental hashing Functions
- init, -- :: Ctx
- update, -- :: Ctx -> ByteString -> Ctx
- finalize, -- :: Ctx -> ByteString
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- hash, -- :: ByteString -> ByteString
- hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
@@ -31,11 +32,33 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString, unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, memcpy)
-data Ctx = Ctx !ByteString
+#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
-digestSize :: Int
-sizeCtx :: Int
+instance Serialize MD2 where
+ get = liftM Digest (getByteString digestSize)
+ put (Digest d) = putByteString d
+
+#endif
+
+data Ctx = Ctx !ByteString
+data MD2 = Digest !ByteString
+ deriving (Eq,Ord,Show)
+digestSize, sizeCtx :: Int
digestSize = 16
sizeCtx = 96
View
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
-- |
-- Module : Crypto.Hash.MD4
@@ -9,17 +9,18 @@
--
-- A module containing MD4 bindings
--
-module Crypto.Hash.MD4 (
- Ctx(..),
+module Crypto.Hash.MD4
+ ( Ctx(..)
+ , MD4
-- * Incremental hashing Functions
- init, -- :: Ctx
- update, -- :: Ctx -> ByteString -> Ctx
- finalize, -- :: Ctx -> ByteString
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- hash, -- :: ByteString -> ByteString
- hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
@@ -31,11 +32,33 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString, unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, memcpy)
-data Ctx = Ctx !ByteString
+#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
-digestSize :: Int
-sizeCtx :: Int
+instance Serialize MD4 where
+ get = liftM Digest (getByteString digestSize)
+ put (Digest d) = putByteString d
+
+#endif
+
+data Ctx = Ctx !ByteString
+data MD4 = Digest !ByteString
+ deriving (Eq,Ord,Show)
+digestSize, sizeCtx :: Int
digestSize = 16
sizeCtx = 96
View
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
-- |
-- Module : Crypto.Hash.MD5
@@ -9,17 +9,18 @@
--
-- A module containing MD5 bindings
--
-module Crypto.Hash.MD5 (
- Ctx(..),
+module Crypto.Hash.MD5
+ ( Ctx(..)
+ , MD5
-- * Incremental hashing Functions
- init, -- :: Ctx
- update, -- :: Ctx -> ByteString -> Ctx
- finalize, -- :: Ctx -> ByteString
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- hash, -- :: ByteString -> ByteString
- hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
@@ -31,11 +32,33 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString, unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, memcpy)
-data Ctx = Ctx !ByteString
+#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
-digestSize :: Int
-sizeCtx :: Int
+instance Serialize MD5 where
+ get = liftM Digest (getByteString digestSize)
+ put (Digest d) = putByteString d
+
+#endif
+
+data Ctx = Ctx !ByteString
+data MD5 = Digest !ByteString
+ deriving (Eq,Ord,Show)
+digestSize, sizeCtx :: Int
digestSize = 16
sizeCtx = 96
View
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
-- |
-- Module : Crypto.Hash.RIPEMD160
@@ -9,17 +9,18 @@
--
-- A module containing RIPEMD160 bindings
--
-module Crypto.Hash.RIPEMD160 (
- Ctx(..),
+module Crypto.Hash.RIPEMD160
+ ( Ctx(..)
+ , RIPEMD160
-- * Incremental hashing Functions
- init, -- :: Ctx
- update, -- :: Ctx -> ByteString -> Ctx
- finalize, -- :: Ctx -> ByteString
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- hash, -- :: ByteString -> ByteString
- hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
@@ -31,11 +32,33 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString, unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, memcpy)
-data Ctx = Ctx !ByteString
+#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
-digestSize :: Int
-sizeCtx :: Int
+instance Serialize RIPEMD160 where
+ get = liftM Digest (getByteString digestSize)
+ put (Digest d) = putByteString d
+
+#endif
+
+data Ctx = Ctx !ByteString
+data RIPEMD160 = Digest !ByteString
+ deriving (Eq,Ord,Show)
+digestSize, sizeCtx :: Int
digestSize = 20
sizeCtx = 128
View
@@ -1,4 +1,4 @@
-{-# LANGUAGE ForeignFunctionInterface #-}
+{-# LANGUAGE ForeignFunctionInterface, CPP, MultiParamTypeClasses #-}
-- |
-- Module : Crypto.Hash.SHA1
@@ -9,17 +9,18 @@
--
-- A module containing SHA1 bindings
--
-module Crypto.Hash.SHA1 (
- Ctx(..),
+module Crypto.Hash.SHA1
+ ( Ctx(..)
+ , SHA1
-- * Incremental hashing Functions
- init, -- :: Ctx
- update, -- :: Ctx -> ByteString -> Ctx
- finalize, -- :: Ctx -> ByteString
+ , init -- :: Ctx
+ , update -- :: Ctx -> ByteString -> Ctx
+ , finalize -- :: Ctx -> ByteString
-- * Single Pass hashing
- hash, -- :: ByteString -> ByteString
- hashlazy -- :: ByteString -> ByteString
+ , hash -- :: ByteString -> ByteString
+ , hashlazy -- :: ByteString -> ByteString
) where
import Prelude hiding (init)
@@ -31,11 +32,33 @@ import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString, unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, memcpy)
-data Ctx = Ctx !ByteString
+#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
-digestSize :: Int
-sizeCtx :: Int
+instance Serialize SHA1 where
+ get = liftM Digest (getByteString digestSize)
+ put (Digest d) = putByteString d
+
+#endif
+
+data Ctx = Ctx !ByteString
+data SHA1 = Digest !ByteString
+ deriving (Eq,Ord,Show)
+digestSize, sizeCtx :: Int
digestSize = 20
sizeCtx = 96
Oops, something went wrong.

0 comments on commit 3d6bc5a

Please sign in to comment.