/
encrypt.go
96 lines (89 loc) · 1.87 KB
/
encrypt.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package drcom
import (
"encoding/hex"
"math/big"
"strings"
)
func (c *Client) md5(items ...[]byte) (ret []byte) {
for _, v := range items {
c.md5Ctx.Write(v)
}
ret = c.md5Ctx.Sum(nil)
c.md5Ctx.Reset()
return
}
// MACHex2Bytes convert mac address to bytes, the input mac format should be 2a:1b:4c:fe:a9:e9.
func MACHex2Bytes(mac string) (res []byte, err error) {
as := strings.Replace(mac, ":", "", -1)
res = make([]byte, 0, 6)
return hex.DecodeString(as)
}
func (c *Client) ror(md5a, password []byte) (ret []byte) {
l := len(password)
ret = make([]byte, l)
for i := 0; i < l; i++ {
x := md5a[i] ^ password[i]
ret[i] = (byte)((x << 3) + (x >> 5))
}
return
}
func (c *Client) checkSum(data []byte) (ret []byte) {
// 1234 = 0x_00_00_04_d2
sum := []byte{0x00, 0x00, 0x04, 0xd2}
l := len(data)
i := 0
//0123_4567_8901_23
for ; i+3 < l; i = i + 4 {
//abcd ^ 3210
//abcd ^ 7654
//abcd ^ 1098
sum[0] ^= data[i+3]
sum[1] ^= data[i+2]
sum[2] ^= data[i+1]
sum[3] ^= data[i]
}
if i < l {
//剩下_23
//i=12,len=14
tmp := make([]byte, 4)
for j := 3; j >= 0 && i < l; j-- {
//j=3 tmp = 0 0 0 2 i=12 13
//j=2 tmp = 0 0 3 2 i=13 14
tmp[j] = data[i]
i++
}
for j := 0; j < 4; j++ {
sum[j] ^= tmp[j]
}
}
var x = big.NewInt(int64(0))
tmpBytes := x.SetBytes(sum).Mul(x, _magic1).Add(x, _magic2).Bytes()
l = len(tmpBytes)
i = 0
ret = make([]byte, 4)
for j := l - 1; j >= 0 && i < 4; j-- {
ret[i] = tmpBytes[j]
i++
}
return
}
func (c *Client) extra() bool {
return c.Count%21 == 0
}
func (c *Client) crc(buf []byte) (ret []byte) {
sum := make([]byte, 2)
l := len(buf)
for i := 0; i < l-1; i += 2 {
sum[0] ^= buf[i+1]
sum[1] ^= buf[i]
}
x := big.NewInt(int64(0))
tmpBytes := x.SetBytes(sum).Mul(x, _magic3).Bytes()
ret = make([]byte, 4)
l = len(tmpBytes)
for i := 0; i < 4 && l > 0; i++ {
l--
ret[i] = tmpBytes[l]
}
return
}