-
Notifications
You must be signed in to change notification settings - Fork 0
/
XorProtectionContext.js
executable file
·64 lines (57 loc) · 1.7 KB
/
XorProtectionContext.js
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
const Network = require("./Network");
class XorProtectionContext {
/**
* @param {Array} data
* @param {Boolean} serverSide
*/
constructor(data, serverSide = false) {
this.serverSequence = [];
this.serverSelector = 0;
this.clientSequence = [];
this.clientSelector = 0;
this.initialSeed = 0;
let i = 0;
while (i < data.length) {
this.initialSeed ^= data[i];
i++;
}
let j = 0;
while (j < 8)//SEQUENCE_LENGTH
{
if (serverSide) {
this.clientSequence[j] = this.initialSeed ^ j << 3 ^ 87;
this.serverSequence[j] = this.initialSeed ^ j << 3;
}
else {
this.clientSequence[j] = this.initialSeed ^ j << 3;
this.serverSequence[j] = this.initialSeed ^ j << 3 ^ 87;
}
j++;
}
}
/**
* @param {Buffer} data
*/
encrypt(data) {
data.position = 0;
for (let i = Network.PACKET_HEADER_SIZE; i < data.length; i++) {
const value = data[i];
data[i] = value ^ this.serverSequence[this.serverSelector];
this.serverSequence[this.serverSelector] = value;
this.serverSelector ^= value & 7;
}
}
/**
* @param {Buffer} data
* @param {number} length
*/
decrypt(data, length) {
for (let i = Network.PACKET_HEADER_SIZE; i < length; i++) {
const value = data[i];
this.clientSequence[this.clientSelector] = value ^ this.clientSequence[this.clientSelector];
data[i] = this.clientSequence[this.clientSelector];
this.clientSelector ^= this.clientSequence[this.clientSelector] & 7;
}
}
}
module.exports = XorProtectionContext;