Fix: Add input validation for XOR Checksum blocksize (#2537)

This commit is contained in:
dweep 2026-06-09 16:24:47 +05:30
parent 5105aad91d
commit a3a69c7b64

View File

@ -7,53 +7,60 @@
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs"; import Utils from "../Utils.mjs";
import { toHex } from "../lib/Hex.mjs"; import { toHex } from "../lib/Hex.mjs";
import OperationError from "../errors/OperationError.mjs"; // 1. Added import
/** /**
* XOR Checksum operation * XOR Checksum operation
*/ */
class XORChecksum extends Operation { class XORChecksum extends Operation {
/**
* XORChecksum constructor
*/
constructor() {
super();
/** this.name = "XOR Checksum";
* XORChecksum constructor this.module = "Crypto";
*/ this.description =
constructor() { "XOR Checksum splits the input into blocks of a configurable size and performs the XOR operation on these blocks.";
super(); this.infoURL = "https://wikipedia.org/wiki/XOR";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
name: "Blocksize",
type: "number",
value: 4,
},
];
}
this.name = "XOR Checksum"; /**
this.module = "Crypto"; * @param {ArrayBuffer} input
this.description = "XOR Checksum splits the input into blocks of a configurable size and performs the XOR operation on these blocks."; * @param {Object[]} args
this.infoURL = "https://wikipedia.org/wiki/XOR"; * @returns {string}
this.inputType = "ArrayBuffer"; */
this.outputType = "string"; run(input, args) {
this.args = [ const blocksize = args[0];
{
name: "Blocksize", // 2. Added validation check
type: "number", if (!Number.isInteger(blocksize) || blocksize <= 0) {
value: 4 throw new OperationError("Blocksize must be a positive integer.");
},
];
} }
/** input = new Uint8Array(input);
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const blocksize = args[0];
input = new Uint8Array(input);
const res = Array(blocksize); const res = Array(blocksize);
res.fill(0); res.fill(0);
for (const chunk of Utils.chunked(input, blocksize)) { for (const chunk of Utils.chunked(input, blocksize)) {
for (let i = 0; i < blocksize; i++) { for (let i = 0; i < blocksize; i++) {
res[i] ^= chunk[i]; res[i] ^= chunk[i];
} }
}
return toHex(res, "");
} }
return toHex(res, "");
}
} }
export default XORChecksum; export default XORChecksum;