1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-06 03:54:22 -04:00
v2fly/transport/internet/kcp/crypt.go

64 lines
1.2 KiB
Go
Raw Normal View History

2016-06-14 17:25:06 -04:00
package kcp
2016-06-17 10:51:41 -04:00
import (
"hash/fnv"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/serial"
2016-08-06 15:59:22 -04:00
"github.com/v2ray/v2ray-core/transport/internet"
2016-06-17 10:51:41 -04:00
)
type SimpleAuthenticator struct{}
2016-08-06 15:59:22 -04:00
func NewSimpleAuthenticator() internet.Authenticator {
2016-06-17 10:51:41 -04:00
return &SimpleAuthenticator{}
2016-06-14 17:25:06 -04:00
}
2016-08-06 15:59:22 -04:00
func (this *SimpleAuthenticator) Overhead() int {
2016-06-17 10:51:41 -04:00
return 6
2016-06-14 17:25:06 -04:00
}
2016-06-17 10:51:41 -04:00
func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer) {
2016-06-26 16:34:48 -04:00
buffer.PrependUint16(uint16(buffer.Len()))
2016-06-17 10:51:41 -04:00
fnvHash := fnv.New32a()
fnvHash.Write(buffer.Value)
2016-07-17 17:11:05 -04:00
buffer.PrependHash(fnvHash)
2016-06-17 10:51:41 -04: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 10:51:41 -04:00
}
2016-06-14 17:25:06 -04:00
}
2016-06-17 10:51:41 -04: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 10:51:41 -04: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
}