-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
executable file
·141 lines (129 loc) · 2.92 KB
/
uuid.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Package uuid implements a simple, panic-free generator for a uuid v4 based
// on the AES family of block ciphers. The generator never returns an error.
package uuid
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"sync/atomic"
)
const (
ng = 4
ngmask = ng - 1
)
var (
access [ng]uint32
generators [ng]gen
)
func init() {
if (ng & ngmask) != 0 {
panic("uuid: you modified ng to create more gens, but it needs to be a power of 2")
}
}
// V4 returns a UUIDv4. It never returns an error, never panics,
// and never runs out of entropy.
func V4() string {
i := 0
for {
// this is a spinlock, nothing starves these generators
// enough for anything more elaborate, as V4 completes
// in 25-100ns on modern systems
if atomic.CompareAndSwapUint32(&access[i], 0, 1) {
u := string(generators[i].V4())
atomic.StoreUint32(&access[i], 0)
return u
}
i = (i + 1) & ngmask
}
}
// Valid returns true if and only if the input string looks like a UUID
// It examines the length, number of dashes, their location, as well
// as the hexidecimal structure of the input string. For alphanumeric
// hex characters, both lower and upper case is allowed. It does not
// check for version bits or any other semantics not mentioned in this
// comment.
func Valid(s string) bool {
if len(s) != 36 {
return false
}
for _, n := range [...]int{8, 4, 4, 4} {
if !acceptRun(s[:n]) {
return false // ErrFormat
}
if s[n] != '-' {
return false // ErrFieldCount
}
s = s[n+1:]
}
if !acceptRun(s) {
return false // ErrFormat
}
return true
}
func acceptRun(s string) bool {
for _, c := range []byte(s) {
if c >= '0' && c <= '9' {
continue
}
if c >= 'a' && c <= 'f' {
continue
}
if c >= 'A' && c <= 'F' {
continue
}
return false
}
return true
}
func init() {
for i := range generators {
g := newGen()
generators[i] = *g
}
}
type gen struct {
c [36]byte
cipher.BlockMode
_ [8]byte
}
func newGen() *gen {
g := &gen{}
var k [16 * 2]byte
_, err := rand.Read(k[:])
if err != nil {
panic("uuid: failed to read 32 bytes of entropy")
}
block, err := aes.NewCipher(k[:16])
if err != nil {
panic("uuid: failed to initialize aes128")
}
g.BlockMode = cipher.NewCBCEncrypter(block, k[16:32])
return g
}
var h = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
func (g *gen) V4() []byte {
c := g.c[:]
g.CryptBlocks(c[:16], c[:16])
return append(c[:0],
h[c[0]&15], h[c[0]>>4],
h[c[1]&15], h[c[1]>>4],
h[c[2]&15], h[c[2]>>4],
h[c[3]&15], h[c[3]>>4],
'-',
h[c[4]&15], h[c[4]>>4],
h[c[5]&15], h[c[5]>>4],
'-',
'4', h[c[6]>>4], // version 4
h[c[7]&15], h[c[7]>>4],
'-',
h[8|c[8]&3], h[c[8]>>4], // clock seq hi begins with binary 10
h[c[9]&15], h[c[9]>>4],
'-',
h[c[10]>>4], h[c[10]&15],
h[c[11]>>4], h[c[11]&15],
h[c[12]>>4], h[c[12]&15],
h[c[13]>>4], h[c[13]&15],
h[c[14]>>4], h[c[14]&15],
h[c[15]>>4], h[c[15]&15],
)
}