From 6b529cc79d61b97ef47f9b8835d6b14afa84f6c1 Mon Sep 17 00:00:00 2001 From: Spencer Walden Date: Fri, 31 Mar 2023 00:11:22 -0700 Subject: [PATCH] Fix dumb mistakes in transcribing old code --- src/core/operations/XOR.mjs | 117 ++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 37 deletions(-) diff --git a/src/core/operations/XOR.mjs b/src/core/operations/XOR.mjs index 7f6f8e50..b8884419 100644 --- a/src/core/operations/XOR.mjs +++ b/src/core/operations/XOR.mjs @@ -12,7 +12,6 @@ import { bitOp, xor, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp.mjs"; * XOR operation */ class XOR extends Operation { - /** * XOR constructor */ @@ -21,27 +20,36 @@ class XOR extends Operation { this.name = "XOR"; this.module = "Default"; - this.description = "XOR the input with the given key.
e.g. fe023da5

Options
Null preserving: If the current byte is 0x00 or the same as the key, skip it.

Scheme:"; + this.description = + "XOR the input with the given key.
e.g. fe023da5

Options
Null preserving: If the current byte is 0x00 or the same as the key, skip it.

Scheme:"; this.infoURL = "https://wikipedia.org/wiki/XOR"; this.inputType = "ArrayBuffer"; this.outputType = "byteArray"; this.args = [ { - "name": "Key", - "type": "toggleString", - "value": "", - "toggleValues": BITWISE_OP_DELIMS + name: "Key", + type: "toggleString", + value: "", + toggleValues: BITWISE_OP_DELIMS, }, { - "name": "Scheme", - "type": "option", - "value": ["Standard", "Input differential", "Output differential", "Cascade", "Rolling", "Rolling cumulative", "Rolling cumulative (self)"] + name: "Scheme", + type: "option", + value: [ + "Standard", + "Input differential", + "Output differential", + "Cascade", + "Rolling", + "Rolling cumulative", + "Rolling cumulative (self)", + ], }, { - "name": "Null preserving", - "type": "boolean", - "value": false - } + name: "Null preserving", + type: "boolean", + value: false, + }, ]; } @@ -52,33 +60,69 @@ class XOR extends Operation { */ run(input, args) { input = new Uint8Array(input); - const key = Utils.convertToByteArray(args[0].string || "", args[0].option), + const key = Utils.convertToByteArray( + args[0].string || "", + args[0].option + ), [, scheme, nullPreserving] = args; - if (scheme.startswith("Rolling") && key.length) { - const inputChunks = Utils.chunked(input, key.length); - let runningIndex = 0; - let runningKey = key; - let xorred = null; - return inputChunks.reduce((result, current, index) => { - runningIndex += index; - switch (scheme) { - case "Rolling": // key = key + index - return result.concat(bitOp(current, key.map(x => add(x, index)), xor, nullPreserving, scheme)); - case "Rolling cumulative": // key = key + index + previous - return result.concat(bitOp(current, key.map(x => add(x, runningIndex)), xor, nullPreserving, scheme)); - case "Rolling cumulative (self)": // key = key XOR previous chunk - // Xor this chunk - xorred = bitOp(current, runningKey, xor, nullPreserving, scheme)); + if (scheme.startsWith("Rolling") && key.length) { + const inputChunks = Utils.chunked(input, key.length); + let runningIndex = 0; + let runningKey = key; + let xorred = null; + return inputChunks.reduce((result, current, index) => { + runningIndex += index; + switch (scheme) { + // key = key + index + case "Rolling": + return result.concat( + bitOp( + current, + key.map((x) => add(x, index)), + xor, + nullPreserving, + scheme + ) + ); - // Update the running key for next part of loop - runningKey = bitOp(runningKey, current, xor, nullPreserving, scheme)); + // key = key + index + previous + case "Rolling cumulative": + return result.concat( + bitOp( + current, + key.map((x) => add(x, runningIndex)), + xor, + nullPreserving, + scheme + ) + ); - // Return the result with the newest xor'd chunk - return result.concat(xorred); - } - }, Utils.strToByteArray("")); - } + // key = key XOR previous chunk + case "Rolling cumulative (self)": + // Xor this chunk + xorred = bitOp( + current, + runningKey, + xor, + nullPreserving, + scheme + ); + + // Update the running key for next part of loop + runningKey = bitOp( + runningKey, + current, + xor, + nullPreserving, + scheme + ); + + // Return the result with the newest xor'd chunk + return result.concat(xorred); + } + }, Utils.strToByteArray("")); // Start our reduction with an empty byte array + } return bitOp(input, key, xor, nullPreserving, scheme); } @@ -108,7 +152,6 @@ class XOR extends Operation { highlightReverse(pos, args) { return pos; } - } export default XOR;