-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
48 lines (41 loc) · 1.07 KB
/
helpers.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
package furydb
import (
"crypto/rand"
"encoding/hex"
"fmt"
"regexp"
"strings"
)
var regexUUID = regexp.MustCompile("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")
// UUIDStrToBin convert uuid string to bytes
func UUIDStrToBin(txt string) (uid [16]byte, err error) {
dat := strings.ToLower(txt)
if !regexUUID.MatchString(dat) {
return uid, ErrInvalidUUID
}
dat = strings.ReplaceAll(dat, "-", "")
b, err := hex.DecodeString(dat)
if err != nil {
return uid, err
}
// in-place replace
copy(uid[:], b[:])
return uid, nil
}
// UUIDBinToStr convert uuid binary to string
func UUIDBinToStr(uid [16]byte) string {
dst := make([]byte, hex.EncodedLen(len(uid)))
hex.Encode(dst, uid[:])
return fmt.Sprintf("%s-%s-%s-%s-%s", dst[0:8], dst[8:12], dst[12:16], dst[16:20], dst[20:32])
}
// UUIDNewV4 generate random uuid string
func UUIDNewV4() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return "", err
}
var b2 [16]byte
copy(b2[:], b[:])
return UUIDBinToStr(b2), nil
}