9 // GenerateUUID is used to generate a random UUID
10 func GenerateUUID() (string, error) {
11 buf := make([]byte, 16)
12 if _, err := rand.Read(buf); err != nil {
13 return "", fmt.Errorf("failed to read random bytes: %v", err)
16 return FormatUUID(buf)
19 func FormatUUID(buf []byte) (string, error) {
21 return "", fmt.Errorf("wrong length byte slice (%d)", len(buf))
24 return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
32 func ParseUUID(uuid string) ([]byte, error) {
34 return nil, fmt.Errorf("uuid string is wrong length")
39 if uuid[8] != hyph[0] ||
40 uuid[13] != hyph[0] ||
41 uuid[18] != hyph[0] ||
43 return nil, fmt.Errorf("uuid is improperly formatted")
46 hexStr := uuid[0:8] + uuid[9:13] + uuid[14:18] + uuid[19:23] + uuid[24:36]
48 ret, err := hex.DecodeString(hexStr)
53 return nil, fmt.Errorf("decoded hex is the wrong length")