diff --git a/common/uuid/uuid.go b/common/uuid/uuid.go new file mode 100644 index 000000000..39da16e9e --- /dev/null +++ b/common/uuid/uuid.go @@ -0,0 +1,75 @@ +package uuid + +import ( + "crypto/rand" + "encoding/hex" + "errors" +) + +var ( + byteGroups = []int{8, 4, 4, 4, 12} + + InvalidID = errors.New("Invalid ID.") +) + +type UUID struct { + byteValue [16]byte + stringValue string +} + +func (this *UUID) String() string { + return this.stringValue +} + +func (this *UUID) Bytes() []byte { + return this.byteValue[:] +} + +func bytesToString(bytes [16]byte) string { + 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 +} + +func New() *UUID { + var bytes [16]byte + rand.Read(bytes[:]) + return &UUID{ + byteValue: bytes, + stringValue: bytesToString(bytes), + } +} + +func ParseString(str string) (*UUID, error) { + text := []byte(str) + if len(text) < 32 { + return nil, InvalidID + } + + var uuid UUID + uuid.stringValue = str + b := uuid.byteValue[:] + + 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:] + } + + return &uuid, nil +} diff --git a/common/uuid/uuid_test.go b/common/uuid/uuid_test.go new file mode 100644 index 000000000..f1a4b7877 --- /dev/null +++ b/common/uuid/uuid_test.go @@ -0,0 +1,41 @@ +package uuid_test + +import ( + "testing" + + . "github.com/v2ray/v2ray-core/common/uuid" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestParseString(t *testing.T) { + v2testing.Current(t) + + str := "2418d087-648d-4990-86e8-19dca1d006d3" + expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} + + uuid, err := ParseString(str) + assert.Error(err).IsNil() + assert.Bytes(uuid.Bytes()).Equals(expectedBytes) +} + +func TestNewUUID(t *testing.T) { + v2testing.Current(t) + + uuid := New() + uuid2, err := ParseString(uuid.String()) + + assert.Error(err).IsNil() + assert.StringLiteral(uuid.String()).Equals(uuid2.String()) + assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes()) +} + +func TestRandom(t *testing.T) { + v2testing.Current(t) + + uuid := New() + uuid2 := New() + + assert.StringLiteral(uuid.String()).NotEquals(uuid2.String()) + assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes()) +}