Left: | ||
Right: |
OLD | NEW |
---|---|
1 package trivial | 1 package trivial |
2 | 2 |
3 import ( | 3 import ( |
4 "crypto/rand" | 4 "crypto/rand" |
5 "fmt" | 5 "fmt" |
6 "io" | 6 "io" |
7 ) | 7 ) |
8 | 8 |
9 // UUID represent a universal identifier with 16 octets. | 9 // UUID represent a universal identifier with 16 octets. |
10 type UUID [16]byte | 10 type UUID [16]byte |
11 | 11 |
12 // ValidUUID specifies the regular expression that matches an UUID. | |
TheMue
2013/04/09 15:12:22
As told before, that's not correct. A UUID is a 16
dimitern
2013/04/10 12:36:40
The only thing this regexp validates is the UUID g
| |
13 const ValidUUID = "[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[8,9,a,b][0-9a-f]{3}-[0- 9a-f]{12}" | |
14 | |
12 // NewUUID generates a new version 4 UUID relying only on random numbers. | 15 // NewUUID generates a new version 4 UUID relying only on random numbers. |
13 func NewUUID() (UUID, error) { | 16 func NewUUID() (UUID, error) { |
14 uuid := UUID{} | 17 uuid := UUID{} |
15 if _, err := io.ReadFull(rand.Reader, []byte(uuid[0:16])); err != nil { | 18 if _, err := io.ReadFull(rand.Reader, []byte(uuid[0:16])); err != nil { |
16 return UUID{}, err | 19 return UUID{}, err |
17 } | 20 } |
18 // Set version (4) and variant (2) according to RfC 4122. | 21 // Set version (4) and variant (2) according to RfC 4122. |
19 var version byte = 4 << 4 | 22 var version byte = 4 << 4 |
20 var variant byte = 8 << 4 | 23 var variant byte = 8 << 4 |
21 uuid[6] = version | (uuid[6] & 15) | 24 uuid[6] = version | (uuid[6] & 15) |
(...skipping 10 matching lines...) Expand all Loading... | |
32 // Raw returns a copy of the UUID bytes. | 35 // Raw returns a copy of the UUID bytes. |
33 func (uuid UUID) Raw() [16]byte { | 36 func (uuid UUID) Raw() [16]byte { |
34 return [16]byte(uuid) | 37 return [16]byte(uuid) |
35 } | 38 } |
36 | 39 |
37 // String returns a hexadecimal string representation with | 40 // String returns a hexadecimal string representation with |
38 // standardized separators. | 41 // standardized separators. |
39 func (uuid UUID) String() string { | 42 func (uuid UUID) String() string { |
40 return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uu id[8:10], uuid[10:16]) | 43 return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uu id[8:10], uuid[10:16]) |
41 } | 44 } |
OLD | NEW |