Skip to content

Commit bd286aa

Browse files
committed
Merge pull request #2 from schell/issue-1
Issue 1
2 parents ad24e8d + 7ab502d commit bd286aa

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/**
2+
*.sw*
3+

Data/Digest/Pure/MD5.hs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import Data.Word
4545
import Foreign.Storable
4646
import Foreign.Ptr
4747
import Foreign.ForeignPtr
48-
import System.IO
4948
import Data.Binary
5049
import Data.Binary.Get
5150
import Data.Binary.Put
@@ -93,11 +92,10 @@ md5 = hash
9392
md5Finalize :: MD5Context -> B.ByteString -> MD5Digest
9493
md5Finalize !ctx@(MD5Ctx par@(MD5Par a b c d) !totLen) end =
9594
let totLen' = 8*(totLen + fromIntegral l) :: Word64
96-
padBS = P.runPut ( do
97-
P.putByteString end
98-
P.putWord8 0x80
99-
mapM_ P.putWord8 (replicate lenZeroPad 0)
100-
P.putWord64le totLen' )
95+
padBS = P.runPut $ do P.putByteString end
96+
P.putWord8 0x80
97+
mapM_ P.putWord8 (replicate lenZeroPad 0)
98+
P.putWord64le totLen'
10199
in MD5Digest $ blockAndDo par padBS
102100
where
103101
l = B.length end
@@ -112,20 +110,20 @@ md5Finalize !ctx@(MD5Ctx par@(MD5Par a b c d) !totLen) end =
112110
md5Update :: MD5Context -> B.ByteString -> MD5Context
113111
md5Update ctx bs
114112
| B.length bs `rem` blockSizeBytes /= 0 = error "Invalid use of hash update routine (see crypto-api Hash class semantics)"
115-
| otherwise =
113+
| otherwise =
116114
let bs' = if isAligned bs then bs else B.copy bs -- copying has been measured as a net win on my x86 system
117115
new = blockAndDo (mdPartial ctx) bs'
118116
in ctx { mdPartial = new, mdTotalLen = mdTotalLen ctx + fromIntegral (B.length bs) }
119117

120118
blockAndDo :: MD5Partial -> B.ByteString -> MD5Partial
121119
blockAndDo !ctx bs
122120
| B.length bs == 0 = ctx
123-
| otherwise =
121+
| otherwise =
124122
let !new = performMD5Update ctx bs
125123
in blockAndDo new (unsafeDrop blockSizeBytes bs)
126124
{-# INLINE blockAndDo #-}
127125

128-
-- Assumes ByteString length == blockSizeBytes, will fold the
126+
-- Assumes ByteString length == blockSizeBytes, will fold the
129127
-- context across calls to applyMD5Rounds.
130128
performMD5Update :: MD5Partial -> B.ByteString -> MD5Partial
131129
performMD5Update !par@(MD5Par !a !b !c !d) !bs =
@@ -259,7 +257,7 @@ instance Show MD5Digest where
259257
show (MD5Digest h) = show h
260258

261259
instance Show MD5Partial where
262-
show (MD5Par a b c d) =
260+
show (MD5Par a b c d) =
263261
let bs = runPut $ putWord32be d >> putWord32be c >> putWord32be b >> putWord32be a
264262
in foldl' (\str w -> let c = showHex w str
265263
in if length c < length str + 2
@@ -301,11 +299,11 @@ instance S.Serialize MD5Context where
301299

302300
instance S.Serialize MD5Partial where
303301
put (MD5Par a b c d) = P.putWord32le a >> P.putWord32le b >> P.putWord32le c >> P.putWord32le d
304-
get = do a <- G.getWord32le
305-
b <- G.getWord32le
306-
c <- G.getWord32le
307-
d <- G.getWord32le
308-
return $ MD5Par a b c d
302+
get = G.getWord32le >>= (\a ->
303+
G.getWord32le >>= (\b ->
304+
G.getWord32le >>= (\c ->
305+
G.getWord32le >>= (\d ->
306+
return (MD5Par a b c d)))))
309307

310308
instance Hash MD5Context MD5Digest where
311309
outputLength = Tagged 128

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pureMD5
2+
=======
3+
A reasonably performing MD5 implementation in pure Haskell.
4+

0 commit comments

Comments
 (0)