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/vid.go

49 lines
824 B

9 years ago
package core
import (
9 years ago
"crypto/md5"
9 years ago
"encoding/hex"
"fmt"
9 years ago
)
9 years ago
// The ID of en entity, in the form of an UUID.
9 years ago
type VID [16]byte
// Hash generates a MD5 hash based on current VID and a suffix string.
9 years ago
func (v VID) Hash(suffix []byte) []byte {
9 years ago
md5 := md5.New()
md5.Write(v[:])
md5.Write(suffix)
return md5.Sum(nil)
9 years ago
}
9 years ago
var byteGroups = []int{8, 4, 4, 4, 12}
// TODO: leverage a full functional UUID library
func UUIDToVID(uuid string) (v VID, err error) {
9 years ago
text := []byte(uuid)
if len(text) < 32 {
9 years ago
err = fmt.Errorf("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
}