/
uuid.go
47 lines (40 loc) · 1.11 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
package cryptowrapper
import (
"crypto/rand"
"io"
"math/big"
"github.com/WUMUXIAN/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 GenUUID() string {
randBytes := RandBytes(16)
return BytesToUUIDFormat(randBytes)
}
// 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())
}