-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
75 lines (60 loc) · 1.46 KB
/
crypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package kcp
import (
"hash/fnv"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/serial"
)
type Authenticator interface {
HeaderSize() int
// Encrypt encrypts the whole block in src into dst.
// Dst and src may point at the same memory.
Seal(buffer *alloc.Buffer)
// Decrypt decrypts the whole block in src into dst.
// Dst and src may point at the same memory.
Open(buffer *alloc.Buffer) bool
}
type SimpleAuthenticator struct{}
func NewSimpleAuthenticator() Authenticator {
return &SimpleAuthenticator{}
}
func (this *SimpleAuthenticator) HeaderSize() int {
return 6
}
func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer) {
buffer.PrependUint16(uint16(buffer.Len()))
fnvHash := fnv.New32a()
fnvHash.Write(buffer.Value)
buffer.SliceBack(4)
fnvHash.Sum(buffer.Value[:0])
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)
}
}
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)
}
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
}