-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.go
46 lines (42 loc) · 1.2 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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[uuid.go]
// (c) [email protected] License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"crypto/rand"
"fmt"
"strconv"
)
// printUUID generates and prints one or mode UUIDs,
// also known as Universally Unique Identifiers.
// The format is 'XXXXXXXX-XXXX-4XXX-ZXXX-XXXXXXXXXXXX' where every X is a
// random upper-case hex digit, and Z must be one of '8', '9', 'A' or 'B'.
func printUUID(cmd Command, args []string) {
count := 0
for _, arg := range args {
n, _ := strconv.ParseInt(arg, 10, 32) // base 10, bitSize 32
count += int(n)
}
if count == 0 {
count = 1
}
for count > 0 {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
continue
}
// 13th character (at [12]) must be '4'
b[6] = (b[6] | 0x40) & 0x4F
//
// 17th character at [16] must be '8', '9', 'A', or 'B'
b[8] = (b[8] | 0x80) & 0xBF
//
s := fmt.Sprintf("%X-%X-%X-%X-%X",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
fmt.Println(s)
count--
}
}
// end