Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use cereal to access Word32s, add Serilize instances.

Ignore-this: 90ed33746fd8261d54d8ba8fd3c87001

darcs-hash:20100312201157-cef97-3330e37c6a2b4e3751e1ffbaa489988ab3b839ae.gz
  • Loading branch information...
commit 1efa6aaccc2f84e10a181bb730bc19da32810706 1 parent 9ae842a
@TomMD authored
Showing with 40 additions and 11 deletions.
  1. +39 −10 Data/Digest/Pure/MD5.hs
  2. +1 −1  pureMD5.cabal
View
49 Data/Digest/Pure/MD5.hs
@@ -46,7 +46,11 @@ import System.IO
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
+import qualified Data.Serialize.Get as G
+import qualified Data.Serialize.Put as P
+import qualified Data.Serialize as S
import Numeric
+import Data.Array.CArray
-- | Block size in bits
blockSize :: Int
@@ -236,10 +240,12 @@ applyMD5Rounds par@(MD5Par a b c d) w = {-# SCC "applyMD5Rounds" #-}
{-# INLINE (!!) #-}
{-# INLINE applyMD5Rounds #-}
-getNthWord n bs@(PS ptr off len) =
- inlinePerformIO $ withForeignPtr ptr $ \ptr' -> do
- let p = castPtr $ plusPtr ptr' off
- peekElemOff p n
+getNthWord :: Int -> B.ByteString -> Word32
+getNthWord n = right . G.runGet (do
+ G.uncheckedSkip (n * sizeOf (undefined :: Word32))
+ G.getWord32le)
+ where
+ right x = case x of Right y -> y
{-# INLINE getNthWord #-}
infix 9 .<.
@@ -251,12 +257,6 @@ infix 9 .<.
instance Show MD5Digest where
show (MD5Digest h) = show h
-instance Binary MD5Digest where
- put (MD5Digest p) = put p
- get = do
- p <- get
- return $ MD5Digest p
-
instance Show MD5Partial where
show (MD5Par a b c d) =
let bs = runPut $ putWord32be d >> putWord32be c >> putWord32be b >> putWord32be a
@@ -265,6 +265,12 @@ instance Show MD5Partial where
then '0':c
else c) "" (L.unpack bs)
+instance Binary MD5Digest where
+ put (MD5Digest p) = put p
+ get = do
+ p <- get
+ return $ MD5Digest p
+
instance Binary MD5Context where
put (MD5Ctx p r l) = put p >> putWord8 (fromIntegral (B.length r)) >>
putByteString r >> putWord64be l
@@ -281,3 +287,26 @@ instance Binary MD5Partial where
c <- getWord32le
d <- getWord32le
return $ MD5Par a b c d
+
+instance S.Serialize MD5Digest where
+ put (MD5Digest p) = S.put p
+ get = do
+ p <- S.get
+ return $ MD5Digest p
+
+instance S.Serialize MD5Context where
+ put (MD5Ctx p r l) = S.put p >> P.putWord8 (fromIntegral (B.length r)) >>
+ P.putByteString r >> P.putWord64be l
+ get = do p <- S.get
+ s <- G.getWord8
+ r <- G.getByteString (fromIntegral s)
+ l <- G.getWord64be
+ return $ MD5Ctx p r l
+
+instance S.Serialize MD5Partial where
+ put (MD5Par a b c d) = P.putWord32le a >> P.putWord32le b >> P.putWord32le c >> P.putWord32le d
+ get = do a <- G.getWord32le
+ b <- G.getWord32le
+ c <- G.getWord32le
+ d <- G.getWord32le
+ return $ MD5Par a b c d
View
2  pureMD5.cabal
@@ -17,7 +17,7 @@ Flag small_base
Description: Choose the split-up base package.
Library
- Build-Depends: base >= 3 && < 5, bytestring >= 0.9 && < 0.10, binary >= 0.4.0 && < 0.6.0
+ Build-Depends: base == 4.*, bytestring >= 0.9 && < 0.10, binary >= 0.4.0 && < 0.6.0, cereal >= 0.2
hs-source-dirs:
exposed-modules: Data.Digest.Pure.MD5
ghc-options: -O2 -funfolding-use-threshold66 -funfolding-creation-threshold66 -fexcess-precision -funbox-strict-fields
Please sign in to comment.
Something went wrong with that request. Please try again.