-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
41 lines (32 loc) · 846 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
package uuid
import (
"encoding/binary"
"encoding/hex"
)
// NewUUID create Universally unique identifier
func NewUUID() string {
var uuid [16]byte
t, cSeq := getTime()
timeLow := uint32(t)
timeMid := uint16((t >> 32))
timeHi := uint16((t >> 48))
timeHi += 0x1000
binary.BigEndian.PutUint32(uuid[0:], timeLow)
binary.BigEndian.PutUint16(uuid[4:], timeMid)
binary.BigEndian.PutUint16(uuid[6:], timeHi)
binary.BigEndian.PutUint16(uuid[6:], timeHi)
binary.BigEndian.PutUint16(uuid[8:], cSeq)
copy(uuid[10:], node[:6])
return encode(uuid)
}
func encode(uuid [16]byte) string {
dst := make([]byte, hex.EncodedLen(len(uuid)+3))
hex.Encode(dst, uuid[0:4])
dst[8] = '-'
hex.Encode(dst[9:17], uuid[4:8])
dst[17] = '-'
hex.Encode(dst[18:26], uuid[8:12])
dst[26] = '-'
hex.Encode(dst[27:], uuid[12:])
return string(dst[:])
}