forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.go
49 lines (37 loc) · 847 Bytes
/
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
package core
import (
"crypto/md5"
"encoding/hex"
"github.com/v2ray/v2ray-core/log"
)
// The ID of en entity, in the form of an UUID.
type ID [16]byte
// Hash generates a MD5 hash based on current ID and a suffix string.
func (v ID) Hash(suffix []byte) []byte {
md5 := md5.New()
md5.Write(v[:])
md5.Write(suffix)
return md5.Sum(nil)
}
var byteGroups = []int{8, 4, 4, 4, 12}
// TODO: leverage a full functional UUID library
func UUIDToID(uuid string) (v ID, err error) {
text := []byte(uuid)
if len(text) < 32 {
err = log.Error("uuid: invalid UUID string: %s", text)
return
}
b := v[:]
for _, byteGroup := range byteGroups {
if text[0] == '-' {
text = text[1:]
}
_, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
if err != nil {
return
}
text = text[byteGroup:]
b = b[byteGroup/2:]
}
return
}