-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuuid.go
49 lines (40 loc) · 921 Bytes
/
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
package utils
import (
"encoding/hex"
"github.com/fufuok/utils/base58"
)
// UUIDString 随机 UUID
func UUIDString() string {
return B2S(EncodeUUID(UUID()))
}
// UUIDSimple 随机 UUID, 无短横线
func UUIDSimple() string {
return hex.EncodeToString(UUID())
}
// UUIDShort 随机 UUID, 短版, base58
func UUIDShort() string {
return base58.Encode(UUID())
}
// UUID 随机 UUID, RFC4122, Version 4
func UUID() []byte {
id := RandBytes(16)
id[6] = (id[6] & 0x0f) | 0x40 // Version 4
id[8] = (id[8] & 0x3f) | 0x80 // Variant is 10
return id
}
// EncodeUUID 编码 UUID
func EncodeUUID(id []byte) []byte {
src := make([]byte, 16)
copy(src, id)
dst := make([]byte, 36)
hex.Encode(dst, src[:4])
dst[8] = '-'
hex.Encode(dst[9:13], src[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], src[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], src[8:10])
dst[23] = '-'
hex.Encode(dst[24:], src[10:])
return dst
}