1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/uuid/uuid.go

91 lines
1.8 KiB
Go
Raw Normal View History

2021-02-16 15:31:50 -05:00
package uuid
2015-12-12 05:32:40 -05:00
import (
2016-01-08 18:10:57 -05:00
"bytes"
2015-12-12 05:32:40 -05:00
"crypto/rand"
"encoding/hex"
2017-02-22 05:59:01 -05:00
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/errors"
2015-12-12 05:32:40 -05:00
)
var (
byteGroups = []int{8, 4, 4, 4, 12}
)
2016-01-10 18:22:59 -05:00
type UUID [16]byte
2015-12-12 05:32:40 -05:00
2016-04-25 19:08:41 -04:00
// String returns the string representation of this UUID.
2017-02-22 05:59:01 -05: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 05:32:40 -05:00
}
2016-04-25 19:08:41 -04:00
// Bytes returns the bytes representation of this UUID.
2017-02-22 05:59:01 -05:00
func (u *UUID) Bytes() []byte {
return u[:]
2016-01-08 18:10:57 -05:00
}
2016-04-25 19:08:41 -04:00
// Equals returns true if this UUID equals another UUID by value.
2017-02-22 05:59:01 -05:00
func (u *UUID) Equals(another *UUID) bool {
if u == nil && another == nil {
2016-01-08 18:10:57 -05:00
return true
}
2017-02-22 05:59:01 -05:00
if u == nil || another == nil {
2016-01-08 18:10:57 -05:00
return false
}
2017-02-22 05:59:01 -05:00
return bytes.Equal(u.Bytes(), another.Bytes())
2016-01-08 18:10:57 -05:00
}
2018-04-02 03:52:16 -04:00
// New creates a UUID with random value.
2018-01-18 17:25:48 -05:00
func New() UUID {
var uuid UUID
common.Must2(rand.Read(uuid.Bytes()))
2015-12-12 07:11:49 -05:00
return uuid
}
2018-04-02 03:52:16 -04:00
// ParseBytes converts a UUID in byte form to object.
2018-01-18 17:25:48 -05:00
func ParseBytes(b []byte) (UUID, error) {
var uuid UUID
2016-01-10 18:22:59 -05:00
if len(b) != 16 {
2018-01-18 17:25:48 -05:00
return uuid, errors.New("invalid UUID: ", b)
2015-12-12 07:11:49 -05:00
}
2016-01-10 18:22:59 -05:00
copy(uuid[:], b)
return uuid, nil
2015-12-12 05:32:40 -05:00
}
2018-04-02 03:52:16 -04:00
// ParseString converts a UUID in string form to object.
2018-01-18 17:25:48 -05:00
func ParseString(str string) (UUID, error) {
var uuid UUID
2015-12-12 05:32:40 -05:00
text := []byte(str)
if len(text) < 32 {
2018-01-18 17:25:48 -05:00
return uuid, errors.New("invalid UUID: ", str)
2015-12-12 05:32:40 -05:00
}
2016-01-10 18:22:59 -05:00
b := uuid.Bytes()
2015-12-12 05:32:40 -05:00
for _, byteGroup := range byteGroups {
if text[0] == '-' {
text = text[1:]
}
2018-04-11 10:15:29 -04:00
if _, err := hex.Decode(b[:byteGroup/2], text[:byteGroup]); err != nil {
2018-01-18 17:25:48 -05:00
return uuid, err
2015-12-12 05:32:40 -05:00
}
text = text[byteGroup:]
b = b[byteGroup/2:]
}
2015-12-12 07:11:49 -05:00
return uuid, nil
2015-12-12 05:32:40 -05:00
}