forked from twinj/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
71 lines (63 loc) · 1.88 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package uuid
// Version represents the type of UUID.
type Version int
// The following are the supported Versions.
const (
VersionUnknown Version = iota // Unknown
VersionOne // Time based
VersionTwo // DCE security via POSIX UIDs
VersionThree // Namespace hash uses MD5
VersionFour // Crypto random
VersionFive // Namespace hash uses SHA-1
)
// The following are the supported Variants.
const (
VariantNCS uint8 = 0x00
VariantRFC4122 uint8 = 0x80 // or and A0 if masked with 1F
VariantMicrosoft uint8 = 0xC0
VariantFuture uint8 = 0xE0
)
const (
// 3f used by RFC4122 although 1f works for all
variantSet = 0x3f
// rather than using 0xc0 we use 0xe0 to retrieve the variant
// The result is the same for all other variants
// 0x80 and 0xa0 are used to identify RFC4122 compliance
variantGet = 0xe0
)
// String returns English description of version.
func (o Version) String() string {
switch o {
case VersionOne:
return "Version 1: Based on a 60 Bit Timestamp."
case VersionTwo:
return "Version 2: Based on DCE security domain and 60 bit timestamp."
case VersionThree:
return "Version 3: Namespace UUID and unique names hashed by MD5."
case VersionFour:
return "Version 4: Crypto-random generated."
case VersionFive:
return "Version 5: Namespace UUID and unique names hashed by SHA-1."
default:
return "Unknown: Not supported"
}
}
func resolveVersion(version uint8) Version {
switch Version(version) {
case VersionOne, VersionTwo, VersionThree, VersionFour, VersionFive:
return Version(version)
default:
return VersionUnknown
}
}
func variant(variant uint8) uint8 {
switch variant & variantGet {
case VariantRFC4122, 0xA0:
return VariantRFC4122
case VariantMicrosoft:
return VariantMicrosoft
case VariantFuture:
return VariantFuture
}
return VariantNCS
}