on cee7ffa help sign in
72 lines 2.0 kB history raw
1-- |
2-- Module : Data.UUID.Named
3-- Copyright : (c) 2008 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/5 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.Named
22 (generateNamed
23 ,namespaceDNS
24 ,namespaceURL
25 ,namespaceOID
26 ,namespaceX500
27 ) where
29import Data.UUID.Internal
31import Control.Applicative ((<*>),(<$>))
32import Data.Binary.Get (runGet, getWord32be)
33import Data.Maybe
34import Data.Word (Word8)
36import qualified Data.ByteString as B
37import qualified Data.ByteString.Lazy as BL
39-- |Generate a 'UUID' within the specified namespace out of the given
40-- object.
41generateNamed :: (B.ByteString -> B.ByteString) -- ^Hash
42 -> Word8 -- ^Version
43 -> UUID -- ^Namespace
44 -> [Word8] -- ^Object
45 -> UUID
46generateNamed hash version namespace object =
47 let chunk = B.pack $ toList namespace ++ object
48 bytes = BL.fromChunks . (:[]) $ hash chunk
49 w = getWord32be
50 unpackBytes = runGet $
51 buildFromWords version <$> w <*> w <*> w <*> w
52 in unpackBytes bytes
55unsafeFromString :: String -> UUID
56unsafeFromString = fromJust . fromString
58-- |The namespace for DNS addresses
59namespaceDNS :: UUID
60namespaceDNS = unsafeFromString "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
62-- |The namespace for URLs
63namespaceURL :: UUID
64namespaceURL = unsafeFromString "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
66-- |The namespace for ISO OIDs
67namespaceOID :: UUID
68namespaceOID = unsafeFromString "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
70-- |The namespace for X.500 DNs
71namespaceX500 :: UUID
72namespaceX500 = unsafeFromString "6ba7b814-9dad-11d1-80b4-00c04fd430c8"