[project @ 1999-10-29 13:59:52 by sof]
[ghc-hetmet.git] / ghc / lib / misc / MD5.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4 \section[md5]{MD5: Message-digest}
5
6 This module provides basic MD5 support for Haskell, using
7 Colin Plumb's C implementation of MD5 to do the Hard Work.
8
9 \begin{code}
10 {-# OPTIONS -#include "cbits/md5.h" #-}
11 module MD5 
12         (
13           digest        -- :: String       -> IO String
14         , digestPS      -- :: PackedString -> IO (ByteArray Int)
15         ) where
16
17
18 import GlaExts
19 import Addr
20 import PackedString
21
22 \end{code}
23
24 \begin{code}
25 digest :: String -> IO String
26 digest str = do
27   ps   <- stToIO (packStringST str)
28   ba   <- digestPS ps
29   let (ByteArray _ _ ba#) = ba
30   baToString ba# 16# 0#
31  where
32   baToString ba# n# i#
33     | n# ==# 0# = return []
34     | otherwise = do
35        let ch# = indexCharArray# ba# i#
36        ls <- baToString ba# (n# -# 1#) (i# +# 1#)
37        return ((C# ch#):ls)
38
39 digestPS :: PackedString -> IO (ByteArray Int)
40 digestPS ps = do
41   ctxt <- stToIO (newCharArray (0::Int,``sizeof(struct MD5Context)''::Int))
42   let len = lengthPS ps
43   _ccall_ MD5Init ctxt
44   (if isCString ps
45     then _ccall_ MD5Update ctxt (psToCString ps) len
46     else _ccall_ MD5Update ctxt (psToByteArray ps) len)
47   dig  <- stToIO (newCharArray (0::Int,16*(``sizeof(unsigned char)''::Int)))
48   _ccall_ MD5Final dig ctxt
49   stToIO (unsafeFreezeByteArray dig)
50
51 \end{code}