From 58fe6df54c35fcab322388c1f412f1aaef80a5f7 Mon Sep 17 00:00:00 2001 From: Subhadeep Date: Tue, 9 Jun 2026 18:51:10 +0530 Subject: [PATCH] Enhance XORChecksum with error handling and validation Added validation for blocksize and imported OperationError for error handling. fixes Issue #2537. --- src/core/operations/XORChecksum.mjs | 77 ++++++++++++++++------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/core/operations/XORChecksum.mjs b/src/core/operations/XORChecksum.mjs index 1603a265..be154802 100644 --- a/src/core/operations/XORChecksum.mjs +++ b/src/core/operations/XORChecksum.mjs @@ -7,53 +7,60 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; import { toHex } from "../lib/Hex.mjs"; +import OperationError from "../errors/OperationError.mjs"; // 1. Added import for error message /** * XOR Checksum operation */ class XORChecksum extends Operation { + /** + * XORChecksum constructor + */ + constructor() { + super(); - /** - * XORChecksum constructor - */ - constructor() { - super(); + this.name = "XOR Checksum"; + this.module = "Crypto"; + this.description = + "XOR Checksum splits the input into blocks of a configurable size and performs the XOR operation on these blocks."; + 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"; - this.description = "XOR Checksum splits the input into blocks of a configurable size and performs the XOR operation on these blocks."; - this.infoURL = "https://wikipedia.org/wiki/XOR"; - this.inputType = "ArrayBuffer"; - this.outputType = "string"; - this.args = [ - { - name: "Blocksize", - type: "number", - value: 4 - }, - ]; + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const blocksize = args[0]; + + // 2. Added validation check + if (!Number.isInteger(blocksize) || blocksize <= 0) { + throw new OperationError("Blocksize must be a positive integer."); } - /** - * @param {ArrayBuffer} input - * @param {Object[]} args - * @returns {string} - */ - run(input, args) { - const blocksize = args[0]; - input = new Uint8Array(input); + input = new Uint8Array(input); - const res = Array(blocksize); - res.fill(0); + const res = Array(blocksize); + res.fill(0); - for (const chunk of Utils.chunked(input, blocksize)) { - for (let i = 0; i < blocksize; i++) { - res[i] ^= chunk[i]; - } - } - - return toHex(res, ""); + for (const chunk of Utils.chunked(input, blocksize)) { + for (let i = 0; i < blocksize; i++) { + res[i] ^= chunk[i]; + } } + + return toHex(res, ""); + } } export default XORChecksum;