2015-06-30 19:38:32 +00:00
|
|
|
package uuid
|
2015-04-19 01:37:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2016-01-20 00:44:33 +00:00
|
|
|
"encoding/hex"
|
2015-04-19 01:37:27 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2016-04-06 00:30:38 +00:00
|
|
|
// GenerateRandomBytes is used to generate random bytes of given size.
|
|
|
|
func GenerateRandomBytes(size int) ([]byte, error) {
|
|
|
|
buf := make([]byte, size)
|
2015-04-19 01:37:27 +00:00
|
|
|
if _, err := rand.Read(buf); err != nil {
|
2016-04-06 00:30:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to read random bytes: %v", err)
|
2016-01-13 18:40:08 +00:00
|
|
|
}
|
2016-04-06 00:30:38 +00:00
|
|
|
return buf, nil
|
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
|
2016-04-06 00:30:38 +00:00
|
|
|
// GenerateUUID is used to generate a random UUID
|
|
|
|
func GenerateUUID() (string, error) {
|
|
|
|
buf, err := GenerateRandomBytes(16)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
return FormatUUID(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatUUID(buf []byte) (string, error) {
|
|
|
|
if len(buf) != 16 {
|
|
|
|
return "", fmt.Errorf("wrong length byte slice (%d)", len(buf))
|
2015-04-19 01:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
2016-01-13 18:40:08 +00:00
|
|
|
buf[10:16]), nil
|
2015-04-19 01:37:27 +00:00
|
|
|
}
|
2016-01-20 00:44:33 +00:00
|
|
|
|
|
|
|
func ParseUUID(uuid string) ([]byte, error) {
|
|
|
|
if len(uuid) != 36 {
|
|
|
|
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] {
|
|
|
|
return nil, fmt.Errorf("uuid is improperly formatted")
|
|
|
|
}
|
|
|
|
|
|
|
|
hexStr := uuid[0:8] + uuid[9:13] + uuid[14:18] + uuid[19:23] + uuid[24:36]
|
|
|
|
|
|
|
|
ret, err := hex.DecodeString(hexStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(ret) != 16 {
|
|
|
|
return nil, fmt.Errorf("decoded hex is the wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|