-
Notifications
You must be signed in to change notification settings - Fork 9
/
v2_hash.go
74 lines (62 loc) · 1.19 KB
/
v2_hash.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
package shadowtls
import (
"crypto/hmac"
"crypto/sha1"
"hash"
"net"
)
type hashReadConn struct {
net.Conn
hmac hash.Hash
}
func newHashReadConn(conn net.Conn, password string) *hashReadConn {
return &hashReadConn{
conn,
hmac.New(sha1.New, []byte(password)),
}
}
func (c *hashReadConn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b)
if err != nil {
return
}
_, err = c.hmac.Write(b[:n])
return
}
func (c *hashReadConn) Sum() []byte {
return c.hmac.Sum(nil)[:8]
}
type hashWriteConn struct {
net.Conn
hmac hash.Hash
hasContent bool
lastSum []byte
}
func newHashWriteConn(conn net.Conn, password string) *hashWriteConn {
return &hashWriteConn{
Conn: conn,
hmac: hmac.New(sha1.New, []byte(password)),
}
}
func (c *hashWriteConn) Write(p []byte) (n int, err error) {
if c.hmac != nil {
if c.hasContent {
c.lastSum = c.Sum()
}
c.hmac.Write(p)
c.hasContent = true
}
return c.Conn.Write(p)
}
func (c *hashWriteConn) Sum() []byte {
return c.hmac.Sum(nil)[:8]
}
func (c *hashWriteConn) LastSum() []byte {
return c.lastSum
}
func (c *hashWriteConn) Fallback() {
c.hmac = nil
}
func (c *hashWriteConn) HasContent() bool {
return c.hasContent
}