This repository was archived by the owner on Jan 27, 2023. It is now read-only.
forked from WUMUXIAN/go-common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
70 lines (58 loc) · 1.52 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cryptowrapper
import (
"crypto/rand"
"io"
"math/big"
"strings"
"github.com/google/uuid"
"github.com/TectusDreamlab/go-common-utils/codec"
)
// RandBytes returns n bytes of cryptographically strong random bytes.
func RandBytes(n int) []byte {
b := make([]byte, n)
io.ReadFull(rand.Reader, b)
return b
}
// RandBigInt generates and return a bigInt 'bits' bits in length
func RandBigInt(bits int) *big.Int {
n := bits / 8
if 0 != bits%8 {
n++
}
b := RandBytes(n)
r := big.NewInt(0).SetBytes(b)
return r
}
// GenUUID generates UUID
func GenUUIDAlt() string {
randBytes := RandBytes(16)
return BytesToUUIDFormat(randBytes)
}
// GenUUID generates UUID
func GenUUID() string {
return uuid.Must(uuid.NewRandom()).String()
}
// BytesToUUIDFormat converts a 16 bytes to UUID format
func BytesToUUIDFormat(bytes []byte) string {
return codec.ToByteArray(bytes[0:4]).Hex() + "-" +
codec.ToByteArray(bytes[4:6]).Hex() + "-" +
codec.ToByteArray(bytes[6:8]).Hex() + "-" +
codec.ToByteArray(bytes[8:10]).Hex() + "-" +
codec.ToByteArray(bytes[10:]).Hex()
}
// MD5UUIDFormat gets the MD5 hash of the input content and convert to UUID Format
func MD5UUIDFormat(input ...[]byte) string {
return BytesToUUIDFormat(codec.GetHash(codec.MD5, input...).Bytes())
}
// Validates UUID
func ValidateUUID(UUID string, sanitize ...bool) (string, error) {
u, err := uuid.Parse(UUID)
if err != nil {
return "", err
}
uStr := u.String()
if len(sanitize) > 0 && sanitize[0] {
uStr = strings.ToLower(uStr)
}
return uStr, err
}