You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/protocol/id.go

72 lines
1.2 KiB

package protocol
9 years ago
import (
"crypto/hmac"
9 years ago
"crypto/md5"
"hash"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/uuid"
9 years ago
)
const (
IDBytesLen = 16
)
var (
InvalidID = errors.New("Invalid ID.")
)
type IDHash func(key []byte) hash.Hash
func DefaultIDHash(key []byte) hash.Hash {
return hmac.New(md5.New, key)
}
9 years ago
// The ID of en entity, in the form of an UUID.
type ID struct {
uuid *uuid.UUID
cmdKey [IDBytesLen]byte
}
8 years ago
func (v *ID) Equals(another *ID) bool {
return v.uuid.Equals(another.uuid)
}
8 years ago
func (v *ID) Bytes() []byte {
return v.uuid.Bytes()
}
8 years ago
func (v *ID) String() string {
return v.uuid.String()
}
9 years ago
8 years ago
func (v *ID) UUID() *uuid.UUID {
return v.uuid
}
func (v ID) CmdKey() []byte {
return v.cmdKey[:]
}
func NewID(uuid *uuid.UUID) *ID {
id := &ID{uuid: uuid}
9 years ago
md5hash := md5.New()
md5hash.Write(uuid.Bytes())
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
md5hash.Sum(id.cmdKey[:0])
return id
9 years ago
}
func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID {
alterIDs := make([]*ID, alterIDCount)
prevID := primary.UUID()
for idx := range alterIDs {
newid := prevID.Next()
// TODO: check duplicates
alterIDs[idx] = NewID(newid)
prevID = newid
}
return alterIDs
}