-
Notifications
You must be signed in to change notification settings - Fork 0
/
gws.go
40 lines (35 loc) · 1.12 KB
/
gws.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
package mask
import "encoding/binary"
func MaskXOR(b []byte, key []byte) {
maskKey := binary.LittleEndian.Uint32(key)
key64 := uint64(maskKey)<<32 + uint64(maskKey)
for len(b) >= 64 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
v = binary.LittleEndian.Uint64(b[8:16])
binary.LittleEndian.PutUint64(b[8:16], v^key64)
v = binary.LittleEndian.Uint64(b[16:24])
binary.LittleEndian.PutUint64(b[16:24], v^key64)
v = binary.LittleEndian.Uint64(b[24:32])
binary.LittleEndian.PutUint64(b[24:32], v^key64)
v = binary.LittleEndian.Uint64(b[32:40])
binary.LittleEndian.PutUint64(b[32:40], v^key64)
v = binary.LittleEndian.Uint64(b[40:48])
binary.LittleEndian.PutUint64(b[40:48], v^key64)
v = binary.LittleEndian.Uint64(b[48:56])
binary.LittleEndian.PutUint64(b[48:56], v^key64)
v = binary.LittleEndian.Uint64(b[56:64])
binary.LittleEndian.PutUint64(b[56:64], v^key64)
b = b[64:]
}
for len(b) >= 8 {
v := binary.LittleEndian.Uint64(b[:8])
binary.LittleEndian.PutUint64(b[:8], v^key64)
b = b[8:]
}
n := len(b)
for i := 0; i < n; i++ {
idx := i & 3
b[i] ^= key[idx]
}
}