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

94 lines
1.6 KiB

9 years ago
package core
import (
"crypto/hmac"
9 years ago
"crypto/md5"
9 years ago
"encoding/hex"
"hash"
mrand "math/rand"
"time"
"github.com/v2ray/v2ray-core/log"
9 years ago
)
const (
IDBytesLen = 16
)
9 years ago
// The ID of en entity, in the form of an UUID.
type ID struct {
String string
Bytes []byte
hasher hash.Hash
}
func NewID(id string) (ID, error) {
idBytes, err := UUIDToID(id)
if err != nil {
return ID{}, log.Error("Failed to parse id %s", id)
}
hasher := hmac.New(md5.New, idBytes)
return ID{id, idBytes, hasher}, nil
}
func (v ID) TimeRangeHash(rangeSec int) []byte {
nowSec := time.Now().UTC().Unix()
delta := mrand.Intn(rangeSec*2) - rangeSec
targetSec := nowSec + int64(delta)
return v.TimeHash(targetSec)
}
func (v ID) TimeHash(timeSec int64) []byte {
buffer := []byte{
byte(timeSec >> 56),
byte(timeSec >> 48),
byte(timeSec >> 40),
byte(timeSec >> 32),
byte(timeSec >> 24),
byte(timeSec >> 16),
byte(timeSec >> 8),
byte(timeSec),
}
return v.Hash(buffer)
}
func (v ID) Hash(data []byte) []byte {
9 years ago
v.hasher.Write(data)
hash := v.hasher.Sum(nil)
v.hasher.Reset()
return hash
9 years ago
}
9 years ago
var byteGroups = []int{8, 4, 4, 4, 12}
// TODO: leverage a full functional UUID library
func UUIDToID(uuid string) (v []byte, err error) {
v = make([]byte, 16)
9 years ago
text := []byte(uuid)
if len(text) < 32 {
err = log.Error("uuid: invalid UUID string: %s", text)
9 years ago
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
}