-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.js
111 lines (102 loc) · 9.29 KB
/
checksum.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let received_consts = {};
function mirror_bits(n) {
return parseInt(n.toString(2).split("").reverse().join(""), 2);
}
function count_checksum(number, constant) {
let bytes = number.split(' '); //console.log(bytes);
let rev_bytes = []; bytes.forEach(byte => rev_bytes.push(mirror_bits(byte))); //console.log(rev_bytes);
let buff = 0; rev_bytes.forEach(byte => buff+=byte); //console.log(buff);
//return buff+constant;
return mirror_bits(buff+constant).toString(2);
}
function find_constant(data, valid, index) {
//console.log(`Checking constant for ${data}`);
for (var i=0; i<=0xff; i++) {
var test = count_checksum(data, i);
if (test == valid) {
if (!received_consts[`0x${i.toString(16)}`]) received_consts[`0x${i.toString(16)}`] = 0;
if (!tested_packets[index].possible_const) tested_packets[index].possible_const = [];
received_consts[`0x${i.toString(16)}`]++;
tested_packets[index].possible_const.push(`0x${i.toString(16)}`);
//console.log(`Found possible constant: 0x${i.toString(16)}, result: ${test}`);
}
}
}
const CHECKSUM_CONST = 0x17;
let tested_packets = [
{"packet" : "00100100 10000000 10100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10000000 00100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001"},
{"packet" : "00100100 10000000 10010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 01010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "100001"},
{"packet" : "00100100 10000000 10010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 10000000 10100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10000000 01100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "100001"}, // 25'C
{"packet" : "00100100 10000000 10100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10000000 00100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1"},
{"packet" : "00100100 10000000 10000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 01000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 10000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "101"},
{"packet" : "00100100 10000000 00000000 00000000 00000000 00000000 10000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 01000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10100000 00010000 10000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10100000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10100000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "101001"},
{"packet" : "00100100 10100000 00010000 10000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10100000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10100000 00010000 10101000 00000000 00000000 00000000 00000000", "valid_checksum" : "10100001"},
{"packet" : "00100100 10100000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 10100000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10000000 00100000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001"},
{"packet" : "00100100 10000000 10010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 01010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "100001"},
{"packet" : "00100100 10000000 10010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 10000000 00010000 10000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 10100000 00000000 00000000 00000000 10010010", "valid_checksum" : "1"},
{"packet" : "00100100 10000000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 10101000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010101"},
{"packet" : "00100100 10000000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 00010000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 00010000 00010000 10000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 00010000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "101001"},
{"packet" : "00100100 00010000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 00010000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10000000 00000000 00000000 00000000 00000000 10000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "101"},
{"packet" : "00100100 10000000 10000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 01000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 10000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1"},
{"packet" : "00100100 10000000 10100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10000000 01000000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "100001"},
{"packet" : "00100100 10000000 10100000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 10000000 10010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 01000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1000001"},
{"packet" : "00100100 10100000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 10100000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 00010000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1001001"},
{"packet" : "00100100 10000000 10010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 10000000 00010000 10000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "10001"},
{"packet" : "00100100 10000000 00010000 01000000 00000000 00000000 00000000 00000000", "valid_checksum" : "100001"},
{"packet" : "00100100 10000000 00010000 01010100 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00010000 10010100 00000000 00000000 00000000 00000000", "valid_checksum" : "1010101"},
{"packet" : "00100100 10000000 00010000 10101000 00000000 00000000 00000000 00000000", "valid_checksum" : "10000001"},
{"packet" : "00100100 10000000 00010000 00010100 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00010000 10010100 00000000 00000000 00000000 00000000", "valid_checksum" : "1010101"},
{"packet" : "00100100 10000000 00010000 00010100 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00100100 10000000 00010000 00010100 00000000 00000000 00000000 10010010", "valid_checksum" : "1010001"},
{"packet" : "00100100 10000000 00010000 00010100 00000000 00000000 00000000 00000000", "valid_checksum" : "10101"},
{"packet" : "00000100 10000000 00010000 00000000 00000000 00000000 00000000 00000000", "valid_checksum" : "1"}
];
console.log("STAGE #1, finding possible constants for each packet:");
for (var i in tested_packets) {
find_constant(tested_packets[i]['packet'], tested_packets[i]['valid_checksum'], i);
}
console.log("Result:");
console.log(tested_packets);