From 6768038a2f0dc8d1be46d34078ea3966fbf7ffb2 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Sun, 27 May 2018 16:13:18 +0100 Subject: [PATCH] ESM: Tidied up recently ported ops --- src/core/lib/Code.mjs | 6 ++---- src/core/operations/BlowfishDecrypt.mjs | 3 ++- src/core/operations/BlowfishEncrypt.mjs | 9 +++++++-- src/core/operations/DESDecrypt.mjs | 7 ++++--- src/core/operations/DESEncrypt.mjs | 5 +++-- src/core/operations/DeriveEVPKey.mjs | 3 ++- src/core/operations/DerivePBKDF2Key.mjs | 4 +++- src/core/operations/JavaScriptBeautify.mjs | 3 ++- src/core/operations/ToCamelCase.mjs | 2 +- src/core/operations/ToKebabCase.mjs | 2 +- src/core/operations/ToSnakeCase.mjs | 2 +- src/core/operations/TripleDESDecrypt.mjs | 7 ++++--- src/core/operations/TripleDESEncrypt.mjs | 5 +++-- src/core/operations/XORBruteForce.mjs | 16 ++++++++++++---- 14 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/core/lib/Code.mjs b/src/core/lib/Code.mjs index ee55dfaf..2d749d79 100644 --- a/src/core/lib/Code.mjs +++ b/src/core/lib/Code.mjs @@ -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) { diff --git a/src/core/operations/BlowfishDecrypt.mjs b/src/core/operations/BlowfishDecrypt.mjs index 95ba415d..d349d3c4 100644 --- a/src/core/operations/BlowfishDecrypt.mjs +++ b/src/core/operations/BlowfishDecrypt.mjs @@ -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; diff --git a/src/core/operations/BlowfishEncrypt.mjs b/src/core/operations/BlowfishEncrypt.mjs index 7af32bc8..60c45560 100644 --- a/src/core/operations/BlowfishEncrypt.mjs +++ b/src/core/operations/BlowfishEncrypt.mjs @@ -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; } } diff --git a/src/core/operations/DESDecrypt.mjs b/src/core/operations/DESDecrypt.mjs index 0894dfb8..b1d0d8c7 100644 --- a/src/core/operations/DESDecrypt.mjs +++ b/src/core/operations/DESDecrypt.mjs @@ -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."); } } diff --git a/src/core/operations/DESEncrypt.mjs b/src/core/operations/DESEncrypt.mjs index ca3f52e5..17a2abcf 100644 --- a/src/core/operations/DESEncrypt.mjs +++ b/src/core/operations/DESEncrypt.mjs @@ -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); diff --git a/src/core/operations/DeriveEVPKey.mjs b/src/core/operations/DeriveEVPKey.mjs index ebab6961..ad17f0b1 100644 --- a/src/core/operations/DeriveEVPKey.mjs +++ b/src/core/operations/DeriveEVPKey.mjs @@ -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, diff --git a/src/core/operations/DerivePBKDF2Key.mjs b/src/core/operations/DerivePBKDF2Key.mjs index f9c7cf75..d3f7fe9a 100644 --- a/src/core/operations/DerivePBKDF2Key.mjs +++ b/src/core/operations/DerivePBKDF2Key.mjs @@ -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()); diff --git a/src/core/operations/JavaScriptBeautify.mjs b/src/core/operations/JavaScriptBeautify.mjs index 4c0607d2..a4bf326b 100644 --- a/src/core/operations/JavaScriptBeautify.mjs +++ b/src/core/operations/JavaScriptBeautify.mjs @@ -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.
" + e.message; + throw new OperationError("Unable to parse JavaScript.
" + e.message); } return result; } diff --git a/src/core/operations/ToCamelCase.mjs b/src/core/operations/ToCamelCase.mjs index 5c9fa939..cc42949c 100644 --- a/src/core/operations/ToCamelCase.mjs +++ b/src/core/operations/ToCamelCase.mjs @@ -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"; diff --git a/src/core/operations/ToKebabCase.mjs b/src/core/operations/ToKebabCase.mjs index 5e62460e..c293ede8 100644 --- a/src/core/operations/ToKebabCase.mjs +++ b/src/core/operations/ToKebabCase.mjs @@ -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"; diff --git a/src/core/operations/ToSnakeCase.mjs b/src/core/operations/ToSnakeCase.mjs index 0c138272..10af102b 100644 --- a/src/core/operations/ToSnakeCase.mjs +++ b/src/core/operations/ToSnakeCase.mjs @@ -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"; diff --git a/src/core/operations/TripleDESDecrypt.mjs b/src/core/operations/TripleDESDecrypt.mjs index db9e6cd2..8f5a295d 100644 --- a/src/core/operations/TripleDESDecrypt.mjs +++ b/src/core/operations/TripleDESDecrypt.mjs @@ -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."); } } diff --git a/src/core/operations/TripleDESEncrypt.mjs b/src/core/operations/TripleDESEncrypt.mjs index c1618ca5..2384ea3c 100644 --- a/src/core/operations/TripleDESEncrypt.mjs +++ b/src/core/operations/TripleDESEncrypt.mjs @@ -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); diff --git a/src/core/operations/XORBruteForce.mjs b/src/core/operations/XORBruteForce.mjs index 4ceeb44a..131d5a63 100644 --- a/src/core/operations/XORBruteForce.mjs +++ b/src/core/operations/XORBruteForce.mjs @@ -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 = "";