return buf, nil
}
+const uuidLen = 16
+
// GenerateUUID is used to generate a random UUID
func GenerateUUID() (string, error) {
- buf, err := GenerateRandomBytes(16)
+ buf, err := GenerateRandomBytes(uuidLen)
if err != nil {
return "", err
}
}
func FormatUUID(buf []byte) (string, error) {
- if len(buf) != 16 {
- return "", fmt.Errorf("wrong length byte slice (%d)", len(buf))
+ if buflen := len(buf); buflen != uuidLen {
+ return "", fmt.Errorf("wrong length byte slice (%d)", buflen)
}
- return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
+ return fmt.Sprintf("%x-%x-%x-%x-%x",
buf[0:4],
buf[4:6],
buf[6:8],
}
func ParseUUID(uuid string) ([]byte, error) {
- if len(uuid) != 36 {
+ if len(uuid) != 2 * uuidLen + 4 {
return nil, fmt.Errorf("uuid string is wrong length")
}
- hyph := []byte("-")
-
- if uuid[8] != hyph[0] ||
- uuid[13] != hyph[0] ||
- uuid[18] != hyph[0] ||
- uuid[23] != hyph[0] {
+ if uuid[8] != '-' ||
+ uuid[13] != '-' ||
+ uuid[18] != '-' ||
+ uuid[23] != '-' {
return nil, fmt.Errorf("uuid is improperly formatted")
}
if err != nil {
return nil, err
}
- if len(ret) != 16 {
+ if len(ret) != uuidLen {
return nil, fmt.Errorf("decoded hex is the wrong length")
}