-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
78 lines (67 loc) · 1.5 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
package kgo
import (
"crypto/rand"
"encoding/hex"
"io"
"sync"
)
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
// 4122.
type UUID [16]byte
const randPoolSize = 16 * 16
var (
rander = rand.Reader // random function
poolMu sync.Mutex
poolPos = randPoolSize // protected with poolMu
pool [randPoolSize]byte // protected with poolMu
)
func init() {
newRandomFromPool()
}
func Uuid() string {
uuid := newRandomFromPool()
var buf [36]byte
encodeHex(buf[:], uuid)
return B2S(buf[:])
}
func SimpleUuid() string {
uuid := newRandomFromPool()
var buf [32]byte
encodeHexShort(buf[:], uuid)
return B2S(buf[:])
}
func newRandomFromPool() UUID {
var uuid UUID
poolMu.Lock()
if poolPos == randPoolSize {
if _, err := io.ReadFull(rander, pool[:]); err != nil {
poolMu.Unlock()
panic(err)
}
poolPos = 0
}
copy(uuid[:], pool[poolPos:(poolPos+16)])
poolPos += 16
poolMu.Unlock()
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return uuid
}
func encodeHex(dst []byte, uuid UUID) {
hex.Encode(dst, uuid[:4])
dst[8] = '-'
hex.Encode(dst[9:13], uuid[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], uuid[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], uuid[8:10])
dst[23] = '-'
hex.Encode(dst[24:], uuid[10:])
}
func encodeHexShort(dst []byte, uuid UUID) {
hex.Encode(dst, uuid[:4])
hex.Encode(dst[8:12], uuid[4:6])
hex.Encode(dst[12:16], uuid[6:8])
hex.Encode(dst[16:20], uuid[5:7])
hex.Encode(dst[12:], uuid[6:])
}