on 8917b68 help sign in
67 lines 1.8 kB history raw
1-- |
2-- Module : Data.UUID.V3
3-- Copyright : (c) 2010 Antoine Latter
4--
5-- License : BSD-style
6--
7-- Maintainer : aslatter@gmail.com
8-- Stability : experimental
9-- Portability : portable
10--
11--
12-- This module implements Version 3 UUIDs as specified
13-- in RFC 4122.
14--
15-- These UUIDs identify an object within a namespace,
16-- and are deterministic.
17--
18-- The namespace is identified by a UUID. Several sample
19-- namespaces are enclosed.
21module Data.UUID.V3
22 (generateNamed
23 ,Shared.namespaceDNS
24 ,Shared.namespaceURL
25 ,Shared.namespaceOID
26 ,Shared.namespaceX500
27 ) where
29import Data.Word
30import Data.Bits
32import Data.UUID.Internal
33import qualified Data.UUID.Named as Shared
34import qualified Data.Digest.MD5 as MD5
37-- |Generate a 'UUID' within the specified namespace out of the given
38-- object.
39--
40-- Uses an MD5 hash. The UUID is built from first 128 bits of the hash of
41-- the namespace UUID and the name (as a series of Word8).
42generateNamed :: UUID -- ^Namespace
43 -> [Word8] -- ^Object
44 -> UUID
45generateNamed = Shared.generateNamed hash 3
47hash :: [Word8] -> (Word32, Word32, Word32, Word32)
48hash bytes
49 = case map f $ chunk 4 $ MD5.hash bytes of
50 w1:w2:w3:w4:_ -> (w1, w2, w3, w4)
51 _ -> error "Data.UUID.V3.hash: fatal error"
53 where f [b1, b2, b3, b4]
54 = sum
55 [ fromIntegral b4
56 , fromIntegral b3 `shiftL` 8
57 , fromIntegral b2 `shiftL` 16
58 , fromIntegral b1 `shiftL` 24
59 ]
60 f _ = error "Data.UUID.V3.hash: fatal error"
62chunk :: Int -> [a] -> [[a]]
63chunk n = go n []
64 where go _ [] [] = []
65 go _ ys [] = reverse ys:[]
66 go 0 ys xs = reverse ys:go n [] xs
67 go m ys (x:xs) = go (m-1) (x:ys) xs