1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-30 19:15:23 +00:00
v2fly/id.go

50 lines
847 B
Go
Raw Normal View History

2015-09-05 15:48:38 +00:00
package core
import (
2015-09-07 21:48:37 +00:00
"crypto/md5"
2015-09-06 20:10:42 +00:00
"encoding/hex"
"github.com/v2ray/v2ray-core/log"
2015-09-05 15:48:38 +00:00
)
2015-09-07 10:00:46 +00:00
// The ID of en entity, in the form of an UUID.
2015-09-12 20:11:54 +00:00
type ID [16]byte
2015-09-05 15:48:38 +00:00
2015-09-12 20:11:54 +00:00
// Hash generates a MD5 hash based on current ID and a suffix string.
func (v ID) Hash(suffix []byte) []byte {
2015-09-07 21:48:37 +00:00
md5 := md5.New()
md5.Write(v[:])
md5.Write(suffix)
return md5.Sum(nil)
2015-09-07 21:48:19 +00:00
}
2015-09-05 15:48:38 +00:00
var byteGroups = []int{8, 4, 4, 4, 12}
// TODO: leverage a full functional UUID library
2015-09-12 20:11:54 +00:00
func UUIDToID(uuid string) (v ID, err error) {
2015-09-06 20:10:42 +00:00
text := []byte(uuid)
if len(text) < 32 {
err = log.Error("uuid: invalid UUID string: %s", text)
2015-09-05 15:48:38 +00:00
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
}