forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
67 lines (54 loc) · 1.17 KB
/
id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package protocol
import (
"crypto/hmac"
"crypto/md5"
"hash"
"v2ray.com/core/common/uuid"
)
const (
IDBytesLen = 16
)
type IDHash func(key []byte) hash.Hash
func DefaultIDHash(key []byte) hash.Hash {
return hmac.New(md5.New, key)
}
// The ID of en entity, in the form of an UUID.
type ID struct {
uuid *uuid.UUID
cmdKey [IDBytesLen]byte
}
// Equals returns true if this ID equals to the other one.
func (v *ID) Equals(another *ID) bool {
return v.uuid.Equals(another.uuid)
}
func (v *ID) Bytes() []byte {
return v.uuid.Bytes()
}
func (v *ID) String() string {
return v.uuid.String()
}
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}
md5hash := md5.New()
md5hash.Write(uuid.Bytes())
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
md5hash.Sum(id.cmdKey[:0])
return id
}
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
}