Skip to content

Commit b2045e5

Browse files
authored
Merge pull request #2035 from jg42526/checksum/xor
XOR Checksum operation added
2 parents 64a4bfe + 46762a2 commit b2045e5

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

src/core/config/Categories.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@
448448
"Adler-32 Checksum",
449449
"Luhn Checksum",
450450
"CRC Checksum",
451-
"TCP/IP Checksum"
451+
"TCP/IP Checksum",
452+
"XOR Checksum"
452453
]
453454
},
454455
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @author Thomas Weißschuh [[email protected]]
3+
* @copyright Crown Copyright 2023
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Utils from "../Utils.mjs";
9+
import { toHex } from "../lib/Hex.mjs";
10+
11+
/**
12+
* XOR Checksum operation
13+
*/
14+
class XORChecksum extends Operation {
15+
16+
/**
17+
* XORChecksum constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "XOR Checksum";
23+
this.module = "Crypto";
24+
this.description = "XOR Checksum splits the input into blocks of a configurable size and performs the XOR operation on these blocks.";
25+
this.infoURL = "https://wikipedia.org/wiki/XOR";
26+
this.inputType = "ArrayBuffer";
27+
this.outputType = "string";
28+
this.args = [
29+
{
30+
name: "Blocksize",
31+
type: "number",
32+
value: 4
33+
},
34+
];
35+
}
36+
37+
/**
38+
* @param {ArrayBuffer} input
39+
* @param {Object[]} args
40+
* @returns {string}
41+
*/
42+
run(input, args) {
43+
const blocksize = args[0];
44+
input = new Uint8Array(input);
45+
46+
const res = Array(blocksize);
47+
res.fill(0);
48+
49+
for (const chunk of Utils.chunked(input, blocksize)) {
50+
for (let i = 0; i < blocksize; i++) {
51+
res[i] ^= chunk[i];
52+
}
53+
}
54+
55+
return toHex(res, "");
56+
}
57+
}
58+
59+
export default XORChecksum;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Checksum tests.
3+
*
4+
* @author n1474335 [[email protected]]
5+
* @copyright Crown Copyright 2018
6+
* @license Apache-2.0
7+
*/
8+
import TestRegister from "../../lib/TestRegister.mjs";
9+
10+
const BASIC_STRING = "The ships hung in the sky in much the same way that bricks don't.";
11+
const UTF8_STR = "ნუ პანიკას";
12+
const ALL_BYTES = [
13+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
14+
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
15+
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f",
16+
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f",
17+
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
18+
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f",
19+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f",
20+
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f",
21+
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f",
22+
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f",
23+
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
24+
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
25+
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
26+
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf",
27+
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef",
28+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
29+
].join("");
30+
31+
TestRegister.addTests([
32+
{
33+
name: "XOR Checksum (1): nothing",
34+
input: "",
35+
expectedOutput: "00",
36+
recipeConfig: [
37+
{
38+
"op": "XOR Checksum",
39+
"args": [1]
40+
}
41+
]
42+
},
43+
{
44+
name: "XOR Checksum (1): basic string",
45+
input: BASIC_STRING,
46+
expectedOutput: "08",
47+
recipeConfig: [
48+
{
49+
"op": "XOR Checksum",
50+
"args": [1]
51+
}
52+
]
53+
},
54+
{
55+
name: "XOR Checksum (1): UTF-8",
56+
input: UTF8_STR,
57+
expectedOutput: "df",
58+
recipeConfig: [
59+
{
60+
"op": "XOR Checksum",
61+
"args": [1]
62+
}
63+
]
64+
},
65+
{
66+
name: "XOR Checksum (1): all bytes",
67+
input: ALL_BYTES,
68+
expectedOutput: "00",
69+
recipeConfig: [
70+
{
71+
"op": "XOR Checksum",
72+
"args": [1]
73+
}
74+
]
75+
},
76+
{
77+
name: "XOR Checksum (4): nothing",
78+
input: "",
79+
expectedOutput: "00000000",
80+
recipeConfig: [
81+
{
82+
"op": "XOR Checksum",
83+
"args": [4]
84+
}
85+
]
86+
},
87+
{
88+
name: "XOR Checksum (4): basic string",
89+
input: BASIC_STRING,
90+
expectedOutput: "4918421b",
91+
recipeConfig: [
92+
{
93+
"op": "XOR Checksum",
94+
"args": [4]
95+
}
96+
]
97+
},
98+
{
99+
name: "XOR Checksum (4): UTF-8",
100+
input: UTF8_STR,
101+
expectedOutput: "83a424dc",
102+
recipeConfig: [
103+
{
104+
"op": "XOR Checksum",
105+
"args": [4]
106+
}
107+
]
108+
},
109+
{
110+
name: "XOR Checksum (4): all bytes",
111+
input: ALL_BYTES,
112+
expectedOutput: "00000000",
113+
recipeConfig: [
114+
{
115+
"op": "XOR Checksum",
116+
"args": [4]
117+
}
118+
]
119+
},
120+
]);

0 commit comments

Comments
 (0)