2015-12-12 10:32:40 +00:00
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
2016-01-08 23:10:57 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2015-12-12 10:32:40 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2017-02-22 10:59:01 +00:00
|
|
|
|
2016-12-04 08:10:47 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2015-12-12 10:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
byteGroups = []int{8, 4, 4, 4, 12}
|
|
|
|
)
|
|
|
|
|
2016-01-10 23:22:59 +00:00
|
|
|
type UUID [16]byte
|
2015-12-12 10:32:40 +00:00
|
|
|
|
2016-04-25 23:08:41 +00:00
|
|
|
// String returns the string representation of this UUID.
|
2017-02-22 10:59:01 +00:00
|
|
|
func (u *UUID) String() string {
|
|
|
|
bytes := u.Bytes()
|
|
|
|
result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
|
|
|
|
start := byteGroups[0] / 2
|
|
|
|
for i := 1; i < len(byteGroups); i++ {
|
|
|
|
nBytes := byteGroups[i] / 2
|
|
|
|
result += "-"
|
|
|
|
result += hex.EncodeToString(bytes[start : start+nBytes])
|
|
|
|
start += nBytes
|
|
|
|
}
|
|
|
|
return result
|
2015-12-12 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:08:41 +00:00
|
|
|
// Bytes returns the bytes representation of this UUID.
|
2017-02-22 10:59:01 +00:00
|
|
|
func (u *UUID) Bytes() []byte {
|
|
|
|
return u[:]
|
2016-01-08 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:08:41 +00:00
|
|
|
// Equals returns true if this UUID equals another UUID by value.
|
2017-02-22 10:59:01 +00:00
|
|
|
func (u *UUID) Equals(another *UUID) bool {
|
|
|
|
if u == nil && another == nil {
|
2016-01-08 23:10:57 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-02-22 10:59:01 +00:00
|
|
|
if u == nil || another == nil {
|
2016-01-08 23:10:57 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-02-22 10:59:01 +00:00
|
|
|
return bytes.Equal(u.Bytes(), another.Bytes())
|
2016-01-08 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next generates a deterministic random UUID based on this UUID.
|
2017-02-22 10:59:01 +00:00
|
|
|
func (u *UUID) Next() *UUID {
|
2016-01-08 23:10:57 +00:00
|
|
|
md5hash := md5.New()
|
2017-02-22 10:59:01 +00:00
|
|
|
md5hash.Write(u.Bytes())
|
2016-01-08 23:10:57 +00:00
|
|
|
md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
|
2016-01-12 12:38:47 +00:00
|
|
|
newid := new(UUID)
|
2016-01-08 23:10:57 +00:00
|
|
|
for {
|
2016-01-12 12:38:47 +00:00
|
|
|
md5hash.Sum(newid[:0])
|
2017-02-22 10:59:01 +00:00
|
|
|
if !newid.Equals(u) {
|
2016-01-08 23:10:57 +00:00
|
|
|
return newid
|
|
|
|
}
|
2016-01-09 00:23:38 +00:00
|
|
|
md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
|
2016-01-08 23:10:57 +00:00
|
|
|
}
|
2015-12-12 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:08:41 +00:00
|
|
|
// New creates an UUID with random value.
|
2015-12-12 10:32:40 +00:00
|
|
|
func New() *UUID {
|
2016-01-10 23:22:59 +00:00
|
|
|
uuid := new(UUID)
|
|
|
|
rand.Read(uuid.Bytes())
|
2015-12-12 12:11:49 +00:00
|
|
|
return uuid
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:59:01 +00:00
|
|
|
// ParseBytes converts an UUID in byte form to object.
|
2016-01-10 23:22:59 +00:00
|
|
|
func ParseBytes(b []byte) (*UUID, error) {
|
|
|
|
if len(b) != 16 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, errors.New("invalid UUID: ", b)
|
2015-12-12 12:11:49 +00:00
|
|
|
}
|
2016-01-10 23:22:59 +00:00
|
|
|
uuid := new(UUID)
|
|
|
|
copy(uuid[:], b)
|
|
|
|
return uuid, nil
|
2015-12-12 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:08:41 +00:00
|
|
|
// ParseString converts an UUID in string form to object.
|
2015-12-12 10:32:40 +00:00
|
|
|
func ParseString(str string) (*UUID, error) {
|
|
|
|
text := []byte(str)
|
|
|
|
if len(text) < 32 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, errors.New("invalid UUID: ", str)
|
2015-12-12 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 23:22:59 +00:00
|
|
|
uuid := new(UUID)
|
|
|
|
b := uuid.Bytes()
|
2015-12-12 10:32:40 +00:00
|
|
|
|
|
|
|
for _, byteGroup := range byteGroups {
|
|
|
|
if text[0] == '-' {
|
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := hex.Decode(b[:byteGroup/2], text[:byteGroup])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text[byteGroup:]
|
|
|
|
b = b[byteGroup/2:]
|
|
|
|
}
|
|
|
|
|
2015-12-12 12:11:49 +00:00
|
|
|
return uuid, nil
|
2015-12-12 10:32:40 +00:00
|
|
|
}
|