-
Notifications
You must be signed in to change notification settings - Fork 0
/
guid-generator.go
49 lines (41 loc) · 1.12 KB
/
guid-generator.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 guids
import (
"fmt"
"math/rand"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// GUIDV4 generates a GUID of version 4
func GUIDV4() string {
// Generate 16 random bytes (=128 bits)
bytes := make([]byte, 16)
rand.Read(bytes)
// Adjust certain bits according to RFC 4122 section 4.4 as follows:
// a) set the four most significant bits of the 7th byte to 0100'B, so the high nibble is "4"
// b) set the two most significant bits of the 9th byte to 10'B, so the high nibble will be one of "8", "9", "A", or "B" (see Note 1).
seventhByte := bytes[6]
seventhByte = clearBit(seventhByte, 7)
seventhByte = setBit(seventhByte, 6)
seventhByte = clearBit(seventhByte, 5)
seventhByte = clearBit(seventhByte, 4)
ninthByte := bytes[8]
ninthByte = setBit(ninthByte, 7)
ninthByte = clearBit(ninthByte, 6)
var sb strings.Builder
for i, b := range bytes {
if i == 5 || i == 7 || i == 9 || i == 11 {
sb.WriteString("-")
}
fmt.Fprintf(&sb, "%02x", b)
}
return sb.String()
}
func clearBit(b byte, i int) byte {
return b &^ (1 << i)
}
func setBit(b byte, i int) byte {
return b | (1 << i)
}