File: MD5.hs

package info (click to toggle)
haskell-http 30010004-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 212 kB
  • sloc: haskell: 1,919; sh: 82; makefile: 49
file content (43 lines) | stat: -rw-r--r-- 1,336 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Digest.MD5
-- Copyright   :  (c) Dominic Steinitz 2004
-- License     :  BSD-style (see the file ReadMe.tex)
-- 
-- Maintainer  :  dominic.steinitz@blueyonder.co.uk
-- Stability   :  experimental
-- Portability :  portable
--
-- Takes the MD5 module supplied by Ian Lynagh and wraps it so it
-- takes [Octet] and returns [Octet] where the length of the result
-- is always 16.
-- See <http://web.comlab.ox.ac.uk/oucl/work/ian.lynagh/>
-- and <http://www.ietf.org/rfc/rfc1321.txt>.
--
-----------------------------------------------------------------------------

module Network.HTTP.MD5
   ( hash
   , Octet
   ) where

import Data.Char (chr)
import Data.List (unfoldr)
import Data.Word (Word8)
import Numeric (readHex)

import Network.HTTP.MD5Aux (md5s, Str(Str))

type Octet = Word8

-- | Take [Octet] and return [Octet] according to the standard.
--   The length of the result is always 16 octets or 128 bits as required
--   by the standard.

hash :: [Octet] -> [Octet]
hash xs = 
   unfoldr f $ md5s $ Str $ map (chr . fromIntegral) xs
      where f :: String -> Maybe (Octet,String)
            f []       = Nothing
            f (x:y:zs) = Just (fromIntegral a,zs)
                         where [(a,_)] = readHex (x:y:[])