move uuid.Next to protocol

pull/1059/head
Darien Raymond 7 years ago
parent 413a3a4eb6
commit 67c7d6da4a
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -56,11 +56,25 @@ func NewID(uuid uuid.UUID) *ID {
return id return id
} }
func nextId(u *uuid.UUID) uuid.UUID {
md5hash := md5.New()
common.Must2(md5hash.Write(u.Bytes()))
common.Must2(md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81")))
var newid uuid.UUID
for {
md5hash.Sum(newid[:0])
if !newid.Equals(u) {
return newid
}
common.Must2(md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2")))
}
}
func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID { func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID {
alterIDs := make([]*ID, alterIDCount) alterIDs := make([]*ID, alterIDCount)
prevID := primary.UUID() prevID := primary.UUID()
for idx := range alterIDs { for idx := range alterIDs {
newid := prevID.Next() newid := nextId(&prevID)
// TODO: check duplicates // TODO: check duplicates
alterIDs[idx] = NewID(newid) alterIDs[idx] = NewID(newid)
prevID = newid prevID = newid

@ -2,7 +2,6 @@ package uuid // import "v2ray.com/core/common/uuid"
import ( import (
"bytes" "bytes"
"crypto/md5"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
@ -46,21 +45,6 @@ func (u *UUID) Equals(another *UUID) bool {
return bytes.Equal(u.Bytes(), another.Bytes()) return bytes.Equal(u.Bytes(), another.Bytes())
} }
// Next generates a deterministic random UUID based on this UUID.
func (u *UUID) Next() UUID {
md5hash := md5.New()
common.Must2(md5hash.Write(u.Bytes()))
common.Must2(md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81")))
var newid UUID
for {
md5hash.Sum(newid[:0])
if !newid.Equals(u) {
return newid
}
common.Must2(md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2")))
}
}
// New creates a UUID with random value. // New creates a UUID with random value.
func New() UUID { func New() UUID {
var uuid UUID var uuid UUID

Loading…
Cancel
Save