on master help sign in
clean up V3/V5 uuid generation
commit686b46b50f911d03f0f74726f682cfc0b505e6a7 authorAntoine Latter <aslatter@gmail.com> dateWed 28 Mar 2012, 01:20 (15 years ago) parente24d8b2
4 files changed+15−48
Data/UUID/Internal.hs+1 Data/UUID/Named.hs+12−7 Data/UUID/V3.hs+1−24 Data/UUID/V5.hs+1−17
Data/UUID/Internal.hs+1
@@ -21,6 +21,7 @@ module Data.UUID.Internal
2121 ,toString
2222 ,fromWords
2323 ,toWords
24+ ,toList
2425 ,buildFromBytes
2526 ,buildFromWords
2627 ) where
Data/UUID/Named.hs+12−7
@@ -28,23 +28,28 @@ module Data.UUID.Named
2828
2929import Data.UUID.Internal
3030
31−import Data.Binary
31+import Control.Applicative ((<*>),(<$>))
32+import Data.Binary.Get (runGet, getWord32be)
3233import Data.Maybe
34+import Data.Word (Word8)
3335
34−import qualified Data.ByteString.Lazy as BS
36+import qualified Data.ByteString as B
37+import qualified Data.ByteString.Lazy as BL
3538
3639-- |Generate a 'UUID' within the specified namespace out of the given
3740-- object.
38−generateNamed :: ([Word8] -> (Word32, Word32, Word32, Word32)) -- ^Hash
41+generateNamed :: (B.ByteString -> B.ByteString) -- ^Hash
3942 -> Word8 -- ^Version
4043 -> UUID -- ^Namespace
4144 -> [Word8] -- ^Object
4245 -> UUID
4346generateNamed hash version namespace object =
44− let chunk = BS.unpack (toByteString namespace) ++ object
45− (w1, w2, w3, w4) = hash chunk
46− in buildFromWords version w1 w2 w3 w4
47−
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
4853
4954
5055unsafeFromString :: String -> UUID
Data/UUID/V3.hs+1−24
@@ -27,8 +27,6 @@ module Data.UUID.V3
2727 ) where
2828
2929import Data.Word
30−import Data.Bits
31−import qualified Data.ByteString as B
3230
3331import Data.UUID.Internal
3432import qualified Data.UUID.Named as Shared
@@ -43,26 +41,5 @@ import qualified Crypto.Hash.MD5 as MD5
4341generateNamed :: UUID -- ^Namespace
4442 -> [Word8] -- ^Object
4543 -> UUID
46−generateNamed = Shared.generateNamed hash 3
44+generateNamed = Shared.generateNamed MD5.hash 3
4745
48−hash :: [Word8] -> (Word32, Word32, Word32, Word32)
49−hash bytes
50− = case map f $ chunk 4 $ B.unpack $ MD5.hash $ B.pack bytes of
51− w1:w2:w3:w4:_ -> (w1, w2, w3, w4)
52− _ -> error "Data.UUID.V3.hash: fatal error"
53−
54− where f [b1, b2, b3, b4]
55− = sum
56− [ fromIntegral b4
57− , fromIntegral b3 `shiftL` 8
58− , fromIntegral b2 `shiftL` 16
59− , fromIntegral b1 `shiftL` 24
60− ]
61− f _ = error "Data.UUID.V3.hash: fatal error"
62−
63−chunk :: Int -> [a] -> [[a]]
64−chunk n = go n []
65− where go _ [] [] = []
66− go _ ys [] = reverse ys:[]
67− go 0 ys xs = reverse ys:go n [] xs
68− go m ys (x:xs) = go (m-1) (x:ys) xs
Data/UUID/V5.hs+1−17
@@ -27,8 +27,6 @@ module Data.UUID.V5
2727 ) where
2828
2929import Data.Word
30−import Data.Bits
31−import qualified Data.ByteString as B
3230
3331import Data.UUID.Internal
3432import qualified Data.UUID.Named as Shared
@@ -43,19 +41,5 @@ import qualified Crypto.Hash.SHA1 as SHA1
4341generateNamed :: UUID -- ^Namespace
4442 -> [Word8] -- ^Object
4543 -> UUID
46−generateNamed = Shared.generateNamed hash 5
47− where hash bytes
48− = case B.unpack $ SHA1.hash $ B.pack bytes of
49− a1:b1:c1:d1: a2:b2:c2:d2: a3:b3:c3:d3: a4:b4:c4:d4:_ ->
50− ( toWord a1 b1 c1 d1
51− , toWord a2 b2 c2 d2
52− , toWord a3 b3 c3 d3
53− , toWord a4 b4 c4 d4
54− )
55− _ -> error "Data.UUID.V5.hash: fatal error"
44+generateNamed = Shared.generateNamed SHA1.hash 5
5645
57−toWord :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
58−toWord a b c d = fromIntegral a `shiftL` 24
59− .|. fromIntegral b `shiftL` 16
60− .|. fromIntegral c `shiftL` 8
61− .|. fromIntegral d