64 lines
1.1 KiB
Go
Raw Normal View History

2016-06-14 23:25:06 +02:00
package kcp
2016-06-17 16:51:41 +02:00
import (
"hash/fnv"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/serial"
"v2ray.com/core/transport/internet"
2016-06-17 16:51:41 +02:00
)
type SimpleAuthenticator struct{}
2016-08-06 21:59:22 +02:00
func NewSimpleAuthenticator() internet.Authenticator {
2016-06-17 16:51:41 +02:00
return &SimpleAuthenticator{}
2016-06-14 23:25:06 +02:00
}
2016-08-06 21:59:22 +02:00
func (this *SimpleAuthenticator) Overhead() int {
2016-06-17 16:51:41 +02:00
return 6
2016-06-14 23:25:06 +02:00
}
2016-06-17 16:51:41 +02:00
func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer) {
2016-06-26 22:34:48 +02:00
buffer.PrependUint16(uint16(buffer.Len()))
2016-06-17 16:51:41 +02:00
fnvHash := fnv.New32a()
fnvHash.Write(buffer.Value)
2016-07-17 23:11:05 +02:00
buffer.PrependHash(fnvHash)
2016-06-17 16:51:41 +02:00
len := buffer.Len()
xtra := 4 - len%4
if xtra != 0 {
buffer.Slice(0, len+xtra)
}
xorfwd(buffer.Value)
if xtra != 0 {
buffer.Slice(0, len)
2016-06-17 16:51:41 +02:00
}
2016-06-14 23:25:06 +02:00
}
2016-06-17 16:51:41 +02:00
func (this *SimpleAuthenticator) Open(buffer *alloc.Buffer) bool {
len := buffer.Len()
xtra := 4 - len%4
if xtra != 0 {
buffer.Slice(0, len+xtra)
}
xorbkd(buffer.Value)
if xtra != 0 {
buffer.Slice(0, len)
2016-06-17 16:51:41 +02:00
}
fnvHash := fnv.New32a()
fnvHash.Write(buffer.Value[4:])
if serial.BytesToUint32(buffer.Value[:4]) != fnvHash.Sum32() {
return false
}
length := serial.BytesToUint16(buffer.Value[4:6])
if buffer.Len()-6 != int(length) {
return false
}
buffer.SliceFrom(6)
return true
}