/
uuid.go
49 lines (38 loc) · 1.1 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
package crypto
import (
"encoding/hex"
"fmt"
"math/big"
"strings"
"github.com/0xzer/snapper/snaperror"
"github.com/google/uuid"
)
func EncodeUUID(uuid string) ([]byte, error) {
uuid = strings.ReplaceAll(uuid, "-", "")
if len(uuid) != 32 {
return nil, &snaperror.InvalidUUIDError{ErrorMessage: "uuid must be of length 32 to encode"}
}
bytes, err := hex.DecodeString(uuid)
if err != nil {
return nil, err
}
return bytes, nil
}
func DecodeUUID(bytes []byte) (string, error) {
if len(bytes) != 16 {
return "", &snaperror.InvalidUUIDError{ErrorMessage: "encoded uuid must be of length 16 to decode"}
}
uuid := hex.EncodeToString(bytes)
uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:])
return uuid, nil
}
func GenerateBitUUID() (*big.Int, error) {
uuidString := uuid.New().String()
significantBits := uuidString[0:8] + uuidString[9:13] + uuidString[14:18]
result := new(big.Int)
result, success := result.SetString(significantBits, 16)
if !success {
return nil, fmt.Errorf("failed to convert hex string to big.Int")
}
return result, nil
}