...
1
2
3
4
5 package uuid
6
7 import (
8 "crypto/md5"
9 "crypto/sha1"
10 "hash"
11 )
12
13
14 var (
15 NameSpaceDNS = Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
16 NameSpaceURL = Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))
17 NameSpaceOID = Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))
18 NameSpaceX500 = Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))
19 Nil UUID
20
21
22 Max = UUID{
23 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
24 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
25 }
26 )
27
28
29
30
31
32
33 func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
34 h.Reset()
35 h.Write(space[:])
36 h.Write(data)
37 s := h.Sum(nil)
38 var uuid UUID
39 copy(uuid[:], s)
40 uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
41 uuid[8] = (uuid[8] & 0x3f) | 0x80
42 return uuid
43 }
44
45
46
47
48
49 func NewMD5(space UUID, data []byte) UUID {
50 return NewHash(md5.New(), space, data, 3)
51 }
52
53
54
55
56
57 func NewSHA1(space UUID, data []byte) UUID {
58 return NewHash(sha1.New(), space, data, 5)
59 }
60
View as plain text