Fix dumb mistakes in transcribing old code

This commit is contained in:
Spencer Walden 2023-03-31 00:11:22 -07:00
parent 69d9ad9ab7
commit 6b529cc79d

View file

@ -12,7 +12,6 @@ import { bitOp, xor, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp.mjs";
* XOR operation * XOR operation
*/ */
class XOR extends Operation { class XOR extends Operation {
/** /**
* XOR constructor * XOR constructor
*/ */
@ -21,27 +20,36 @@ class XOR extends Operation {
this.name = "XOR"; this.name = "XOR";
this.module = "Default"; this.module = "Default";
this.description = "XOR the input with the given key.<br>e.g. <code>fe023da5</code><br><br><strong>Options</strong><br><u>Null preserving:</u> If the current byte is 0x00 or the same as the key, skip it.<br><br><u>Scheme:</u><ul><li>Standard - key is unchanged after each round</li><li>Input differential - key is set to the value of the previous unprocessed byte</li><li>Output differential - key is set to the value of the previous processed byte</li><li>Cascade - key is set to the input byte shifted by one</li><li>Rolling - key is set to the value of itself added with the current position in the bytes</li>Rolling cumulative - key is set to the value of itself added with a cumulative addition of the position in the bytes<li></li><li>Rolling cumulative (self) - key is set to the value of itself XOR'd with the previous chunk of bytes (where the chunk size is equal to key size)</li></ul>"; this.description =
"XOR the input with the given key.<br>e.g. <code>fe023da5</code><br><br><strong>Options</strong><br><u>Null preserving:</u> If the current byte is 0x00 or the same as the key, skip it.<br><br><u>Scheme:</u><ul><li>Standard - key is unchanged after each round</li><li>Input differential - key is set to the value of the previous unprocessed byte</li><li>Output differential - key is set to the value of the previous processed byte</li><li>Cascade - key is set to the input byte shifted by one</li><li>Rolling - key is set to the value of itself added with the current position in the bytes</li>Rolling cumulative - key is set to the value of itself added with a cumulative addition of the position in the bytes<li></li><li>Rolling cumulative (self) - key is set to the value of itself XOR'd with the previous chunk of bytes (where the chunk size is equal to key size)</li></ul>";
this.infoURL = "https://wikipedia.org/wiki/XOR"; this.infoURL = "https://wikipedia.org/wiki/XOR";
this.inputType = "ArrayBuffer"; this.inputType = "ArrayBuffer";
this.outputType = "byteArray"; this.outputType = "byteArray";
this.args = [ this.args = [
{ {
"name": "Key", name: "Key",
"type": "toggleString", type: "toggleString",
"value": "", value: "",
"toggleValues": BITWISE_OP_DELIMS toggleValues: BITWISE_OP_DELIMS,
}, },
{ {
"name": "Scheme", name: "Scheme",
"type": "option", type: "option",
"value": ["Standard", "Input differential", "Output differential", "Cascade", "Rolling", "Rolling cumulative", "Rolling cumulative (self)"] value: [
"Standard",
"Input differential",
"Output differential",
"Cascade",
"Rolling",
"Rolling cumulative",
"Rolling cumulative (self)",
],
}, },
{ {
"name": "Null preserving", name: "Null preserving",
"type": "boolean", type: "boolean",
"value": false value: false,
} },
]; ];
} }
@ -52,33 +60,69 @@ class XOR extends Operation {
*/ */
run(input, args) { run(input, args) {
input = new Uint8Array(input); 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; [, scheme, nullPreserving] = args;
if (scheme.startswith("Rolling") && key.length) { if (scheme.startsWith("Rolling") && key.length) {
const inputChunks = Utils.chunked(input, key.length); const inputChunks = Utils.chunked(input, key.length);
let runningIndex = 0; let runningIndex = 0;
let runningKey = key; let runningKey = key;
let xorred = null; let xorred = null;
return inputChunks.reduce((result, current, index) => { return inputChunks.reduce((result, current, index) => {
runningIndex += index; runningIndex += index;
switch (scheme) { switch (scheme) {
case "Rolling": // key = key + index // key = key + index
return result.concat(bitOp(current, key.map(x => add(x, index)), xor, nullPreserving, scheme)); case "Rolling":
case "Rolling cumulative": // key = key + index + previous return result.concat(
return result.concat(bitOp(current, key.map(x => add(x, runningIndex)), xor, nullPreserving, scheme)); bitOp(
case "Rolling cumulative (self)": // key = key XOR previous chunk current,
// Xor this chunk key.map((x) => add(x, index)),
xorred = bitOp(current, runningKey, xor, nullPreserving, scheme)); xor,
nullPreserving,
scheme
)
);
// Update the running key for next part of loop // key = key + index + previous
runningKey = bitOp(runningKey, current, xor, nullPreserving, scheme)); 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 // key = key XOR previous chunk
return result.concat(xorred); case "Rolling cumulative (self)":
} // Xor this chunk
}, Utils.strToByteArray("")); 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); return bitOp(input, key, xor, nullPreserving, scheme);
} }
@ -108,7 +152,6 @@ class XOR extends Operation {
highlightReverse(pos, args) { highlightReverse(pos, args) {
return pos; return pos;
} }
} }
export default XOR; export default XOR;