Use array instead of slice

This commit is contained in:
V2Ray 2015-09-23 22:17:25 +02:00
parent ef790375ee
commit 6ecb18268e
4 changed files with 9 additions and 11 deletions

View File

@ -14,8 +14,8 @@ const (
// The ID of en entity, in the form of an UUID.
type ID struct {
String string
Bytes []byte
cmdKey []byte
Bytes [16]byte
cmdKey [16]byte
}
func NewID(id string) (ID, error) {
@ -25,27 +25,25 @@ func NewID(id string) (ID, error) {
}
md5hash := md5.New()
md5hash.Write(idBytes)
md5hash.Write(idBytes[:])
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
cmdKey := md5.Sum(nil)
return ID{
String: id,
Bytes: idBytes,
cmdKey: cmdKey[:],
cmdKey: cmdKey,
}, nil
}
func (v ID) CmdKey() []byte {
return v.cmdKey
return v.cmdKey[:]
}
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)
func UUIDToID(uuid string) (v [16]byte, err error) {
text := []byte(uuid)
if len(text) < 32 {
err = log.Error("uuid: invalid UUID string: %s", text)

View File

@ -13,5 +13,5 @@ func TestUUIDToID(t *testing.T) {
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
actualBytes, _ := NewID(uuid)
assert.Bytes(actualBytes.Bytes).Named("UUID").Equals(expectedBytes)
assert.Bytes(actualBytes.Bytes[:]).Named("UUID").Equals(expectedBytes)
}

View File

@ -39,7 +39,7 @@ func NewTimedUserSet() UserSet {
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
idHash := NewTimeHash(HMACHash{})
for lastSec < nowSec+cacheDurationSec {
idHash := idHash.Hash(id.Bytes, lastSec)
idHash := idHash.Hash(id.Bytes[:], lastSec)
log.Debug("Valid User Hash: %v", idHash)
us.userHash.Set(string(idHash), indexTimePair{idx, lastSec}, lastSec+2*cacheDurationSec)
lastSec++

View File

@ -172,7 +172,7 @@ func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 u
}
counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
hash := idHash.Hash(request.UserId.Bytes, counter)
hash := idHash.Hash(request.UserId.Bytes[:], counter)
log.Debug("Writing userhash: %v", hash)
buffer = append(buffer, hash...)