ESM: Tidied up recently ported ops

This commit is contained in:
n1474335 2018-05-27 16:13:18 +01:00
parent 905bc6699e
commit 6768038a2f
14 changed files with 47 additions and 27 deletions

View file

@ -1,18 +1,16 @@
/**
* Code functions.
* Code resources.
*
* @author n1474335 [n1474335@gmail.com]
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*
*/
/**
* This tries to rename variable names in a code snippet according to a function.
*
* @param {string} input
* @param {function} replacer - this function will be fed the token which should be renamed.
* @param {function} replacer - This function will be fed the token which should be renamed.
* @returns {string}
*/
export function replaceVariableNames(input, replacer) {

View file

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import { Blowfish } from "../vendor/Blowfish";
import { toBase64 } from "../lib/Base64";
import { toHexFast } from "../lib/Hex";
@ -80,7 +81,7 @@ class BlowfishDecrypt extends Operation {
iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args;
if (key.length === 0) return "Enter a key";
if (key.length === 0) throw new OperationError("Enter a key");
input = inputType === "Raw" ? Utils.strToByteArray(input) : input;

View file

@ -6,12 +6,17 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import { Blowfish } from "../vendor/Blowfish";
import { toBase64 } from "../lib/Base64";
/**
* Lookup table for Blowfish output types.
*/
const BLOWFISH_OUTPUT_TYPE_LOOKUP = {
Base64: 0, Hex: 1, String: 2, Raw: 3
};
/**
* Lookup table for Blowfish modes.
*/
@ -77,7 +82,7 @@ class BlowfishEncrypt extends Operation {
iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args;
if (key.length === 0) return "Enter a key";
if (key.length === 0) throw new OperationError("Enter a key");
input = Utils.convertToByteString(input, inputType);
@ -88,7 +93,7 @@ class BlowfishEncrypt extends Operation {
cipherMode: BLOWFISH_MODE_LOOKUP[mode]
});
return outputType === "Raw" ? Utils.byteArrayToChars(enc) : enc ;
return outputType === "Raw" ? Utils.byteArrayToChars(enc) : enc;
}
}

View file

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import forge from "node-forge/dist/forge.min.js";
/**
@ -66,10 +67,10 @@ class DESDecrypt extends Operation {
[,, mode, inputType, outputType] = args;
if (key.length !== 8) {
return `Invalid key length: ${key.length} bytes
throw new OperationError(`Invalid key length: ${key.length} bytes
DES uses a key length of 8 bytes (64 bits).
Triple DES uses a key length of 24 bytes (192 bits).`;
Triple DES uses a key length of 24 bytes (192 bits).`);
}
input = Utils.convertToByteString(input, inputType);
@ -82,7 +83,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`;
if (result) {
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
} else {
return "Unable to decrypt input with these parameters.";
throw new OperationError("Unable to decrypt input with these parameters.");
}
}

View file

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import forge from "node-forge/dist/forge.min.js";
/**
@ -66,10 +67,10 @@ class DESEncrypt extends Operation {
[,, mode, inputType, outputType] = args;
if (key.length !== 8) {
return `Invalid key length: ${key.length} bytes
throw new OperationError(`Invalid key length: ${key.length} bytes
DES uses a key length of 8 bytes (64 bits).
Triple DES uses a key length of 24 bytes (192 bits).`;
Triple DES uses a key length of 24 bytes (192 bits).`);
}
input = Utils.convertToByteString(input, inputType);

View file

@ -63,7 +63,8 @@ class DeriveEVPKey extends Operation {
run(input, args) {
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
keySize = args[1] / 32,
[,, iterations, hasher, ] = args, //eslint-disable-line array-bracket-spacing
iterations = args[2],
hasher = args[3],
salt = Utils.convertToByteString(args[4].string, args[4].option),
key = CryptoJS.EvpKDF(passphrase, salt, {
keySize: keySize,

View file

@ -62,7 +62,9 @@ class DerivePBKDF2Key extends Operation {
*/
run(input, args) {
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
[, keySize, iterations, hasher, ] = args, //eslint-disable-line array-bracket-spacing
keySize = args[1],
iterations = args[2],
hasher = args[3],
salt = Utils.convertToByteString(args[4].string, args[4].option) ||
forge.random.getBytesSync(keySize),
derivedKey = forge.pkcs5.pbkdf2(passphrase, salt, iterations, keySize / 8, hasher.toLowerCase());

View file

@ -5,6 +5,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import escodegen from "escodegen";
import * as esprima from "esprima";
@ -84,7 +85,7 @@ class JavaScriptBeautify extends Operation {
result = escodegen.generate(AST, options);
} catch (e) {
// Leave original error so the user can see the detail
throw "Unable to parse JavaScript.<br>" + e.message;
throw new OperationError("Unable to parse JavaScript.<br>" + e.message);
}
return result;
}

View file

@ -4,7 +4,7 @@
* @license Apache-2.0
*/
import { camelCase } from "lodash";
import camelCase from "lodash/camelCase";
import Operation from "../Operation";
import { replaceVariableNames } from "../lib/Code";

View file

@ -4,7 +4,7 @@
* @license Apache-2.0
*/
import { kebabCase } from "lodash";
import kebabCase from "lodash/kebabCase";
import Operation from "../Operation";
import { replaceVariableNames } from "../lib/Code";

View file

@ -4,7 +4,7 @@
* @license Apache-2.0
*/
import { snakeCase } from "lodash";
import snakeCase from "lodash/snakeCase";
import Operation from "../Operation";
import { replaceVariableNames } from "../lib/Code";

View file

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import forge from "node-forge/dist/forge.min.js";
/**
@ -68,10 +69,10 @@ class TripleDESDecrypt extends Operation {
outputType = args[4];
if (key.length !== 24) {
return `Invalid key length: ${key.length} bytes
throw new OperationError(`Invalid key length: ${key.length} bytes
Triple DES uses a key length of 24 bytes (192 bits).
DES uses a key length of 8 bytes (64 bits).`;
DES uses a key length of 8 bytes (64 bits).`);
}
input = Utils.convertToByteString(input, inputType);
@ -84,7 +85,7 @@ DES uses a key length of 8 bytes (64 bits).`;
if (result) {
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
} else {
return "Unable to decrypt input with these parameters.";
throw new OperationError("Unable to decrypt input with these parameters.");
}
}

View file

@ -6,6 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import OperationError from "../errors/OperationError";
import forge from "node-forge/dist/forge.min.js";
/**
@ -68,10 +69,10 @@ class TripleDESEncrypt extends Operation {
outputType = args[4];
if (key.length !== 24) {
return `Invalid key length: ${key.length} bytes
throw new OperationError(`Invalid key length: ${key.length} bytes
Triple DES uses a key length of 24 bytes (192 bits).
DES uses a key length of 8 bytes (64 bits).`;
DES uses a key length of 8 bytes (64 bits).`);
}
input = Utils.convertToByteString(input, inputType);

View file

@ -75,10 +75,18 @@ class XORBruteForce extends Operation {
* @returns {string}
*/
run(input, args) {
const [keyLength, sampleLength, sampleOffset, scheme, nullPreserving, printKey, outputHex, /* ignore element */] = args, //eslint-disable-line array-bracket-spacing
crib = args[7].toLowerCase();
const output = [];
const [
keyLength,
sampleLength,
sampleOffset,
scheme,
nullPreserving,
printKey,
outputHex,
rawCrib
] = args,
crib = rawCrib.toLowerCase(),
output = [];
let result,
resultUtf8,
record = "";