mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 23:06:16 -04:00
ESM: Tidied up recently ported ops
This commit is contained in:
parent
905bc6699e
commit
6768038a2f
14 changed files with 47 additions and 27 deletions
|
@ -1,18 +1,16 @@
|
||||||
/**
|
/**
|
||||||
* Code functions.
|
* Code resources.
|
||||||
*
|
*
|
||||||
* @author n1474335 [n1474335@gmail.com]
|
* @author n1474335 [n1474335@gmail.com]
|
||||||
*
|
|
||||||
* @copyright Crown Copyright 2018
|
* @copyright Crown Copyright 2018
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This tries to rename variable names in a code snippet according to a function.
|
* This tries to rename variable names in a code snippet according to a function.
|
||||||
*
|
*
|
||||||
* @param {string} input
|
* @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}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function replaceVariableNames(input, replacer) {
|
export function replaceVariableNames(input, replacer) {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import { Blowfish } from "../vendor/Blowfish";
|
import { Blowfish } from "../vendor/Blowfish";
|
||||||
import { toBase64 } from "../lib/Base64";
|
import { toBase64 } from "../lib/Base64";
|
||||||
import { toHexFast } from "../lib/Hex";
|
import { toHexFast } from "../lib/Hex";
|
||||||
|
@ -80,7 +81,7 @@ class BlowfishDecrypt extends Operation {
|
||||||
iv = Utils.convertToByteArray(args[1].string, args[1].option),
|
iv = Utils.convertToByteArray(args[1].string, args[1].option),
|
||||||
[,, mode, inputType, outputType] = args;
|
[,, 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;
|
input = inputType === "Raw" ? Utils.strToByteArray(input) : input;
|
||||||
|
|
||||||
|
|
|
@ -6,12 +6,17 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import { Blowfish } from "../vendor/Blowfish";
|
import { Blowfish } from "../vendor/Blowfish";
|
||||||
import { toBase64 } from "../lib/Base64";
|
import { toBase64 } from "../lib/Base64";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup table for Blowfish output types.
|
||||||
|
*/
|
||||||
const BLOWFISH_OUTPUT_TYPE_LOOKUP = {
|
const BLOWFISH_OUTPUT_TYPE_LOOKUP = {
|
||||||
Base64: 0, Hex: 1, String: 2, Raw: 3
|
Base64: 0, Hex: 1, String: 2, Raw: 3
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lookup table for Blowfish modes.
|
* Lookup table for Blowfish modes.
|
||||||
*/
|
*/
|
||||||
|
@ -77,7 +82,7 @@ class BlowfishEncrypt extends Operation {
|
||||||
iv = Utils.convertToByteArray(args[1].string, args[1].option),
|
iv = Utils.convertToByteArray(args[1].string, args[1].option),
|
||||||
[,, mode, inputType, outputType] = args;
|
[,, 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);
|
input = Utils.convertToByteString(input, inputType);
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import forge from "node-forge/dist/forge.min.js";
|
import forge from "node-forge/dist/forge.min.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,10 +67,10 @@ class DESDecrypt extends Operation {
|
||||||
[,, mode, inputType, outputType] = args;
|
[,, mode, inputType, outputType] = args;
|
||||||
|
|
||||||
if (key.length !== 8) {
|
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).
|
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);
|
input = Utils.convertToByteString(input, inputType);
|
||||||
|
@ -82,7 +83,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`;
|
||||||
if (result) {
|
if (result) {
|
||||||
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
|
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
|
||||||
} else {
|
} else {
|
||||||
return "Unable to decrypt input with these parameters.";
|
throw new OperationError("Unable to decrypt input with these parameters.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import forge from "node-forge/dist/forge.min.js";
|
import forge from "node-forge/dist/forge.min.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,10 +67,10 @@ class DESEncrypt extends Operation {
|
||||||
[,, mode, inputType, outputType] = args;
|
[,, mode, inputType, outputType] = args;
|
||||||
|
|
||||||
if (key.length !== 8) {
|
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).
|
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);
|
input = Utils.convertToByteString(input, inputType);
|
||||||
|
|
|
@ -63,7 +63,8 @@ class DeriveEVPKey extends Operation {
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
|
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
|
||||||
keySize = args[1] / 32,
|
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),
|
salt = Utils.convertToByteString(args[4].string, args[4].option),
|
||||||
key = CryptoJS.EvpKDF(passphrase, salt, {
|
key = CryptoJS.EvpKDF(passphrase, salt, {
|
||||||
keySize: keySize,
|
keySize: keySize,
|
||||||
|
|
|
@ -62,7 +62,9 @@ class DerivePBKDF2Key extends Operation {
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const passphrase = Utils.convertToByteString(args[0].string, args[0].option),
|
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) ||
|
salt = Utils.convertToByteString(args[4].string, args[4].option) ||
|
||||||
forge.random.getBytesSync(keySize),
|
forge.random.getBytesSync(keySize),
|
||||||
derivedKey = forge.pkcs5.pbkdf2(passphrase, salt, iterations, keySize / 8, hasher.toLowerCase());
|
derivedKey = forge.pkcs5.pbkdf2(passphrase, salt, iterations, keySize / 8, hasher.toLowerCase());
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import escodegen from "escodegen";
|
import escodegen from "escodegen";
|
||||||
import * as esprima from "esprima";
|
import * as esprima from "esprima";
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ class JavaScriptBeautify extends Operation {
|
||||||
result = escodegen.generate(AST, options);
|
result = escodegen.generate(AST, options);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Leave original error so the user can see the detail
|
// 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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { camelCase } from "lodash";
|
import camelCase from "lodash/camelCase";
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import { replaceVariableNames } from "../lib/Code";
|
import { replaceVariableNames } from "../lib/Code";
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { kebabCase } from "lodash";
|
import kebabCase from "lodash/kebabCase";
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import { replaceVariableNames } from "../lib/Code";
|
import { replaceVariableNames } from "../lib/Code";
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { snakeCase } from "lodash";
|
import snakeCase from "lodash/snakeCase";
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import { replaceVariableNames } from "../lib/Code";
|
import { replaceVariableNames } from "../lib/Code";
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import forge from "node-forge/dist/forge.min.js";
|
import forge from "node-forge/dist/forge.min.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,10 +69,10 @@ class TripleDESDecrypt extends Operation {
|
||||||
outputType = args[4];
|
outputType = args[4];
|
||||||
|
|
||||||
if (key.length !== 24) {
|
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).
|
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);
|
input = Utils.convertToByteString(input, inputType);
|
||||||
|
@ -84,7 +85,7 @@ DES uses a key length of 8 bytes (64 bits).`;
|
||||||
if (result) {
|
if (result) {
|
||||||
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
|
return outputType === "Hex" ? decipher.output.toHex() : decipher.output.getBytes();
|
||||||
} else {
|
} else {
|
||||||
return "Unable to decrypt input with these parameters.";
|
throw new OperationError("Unable to decrypt input with these parameters.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
import forge from "node-forge/dist/forge.min.js";
|
import forge from "node-forge/dist/forge.min.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,10 +69,10 @@ class TripleDESEncrypt extends Operation {
|
||||||
outputType = args[4];
|
outputType = args[4];
|
||||||
|
|
||||||
if (key.length !== 24) {
|
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).
|
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);
|
input = Utils.convertToByteString(input, inputType);
|
||||||
|
|
|
@ -75,10 +75,18 @@ class XORBruteForce extends Operation {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const [keyLength, sampleLength, sampleOffset, scheme, nullPreserving, printKey, outputHex, /* ignore element */] = args, //eslint-disable-line array-bracket-spacing
|
const [
|
||||||
crib = args[7].toLowerCase();
|
keyLength,
|
||||||
|
sampleLength,
|
||||||
const output = [];
|
sampleOffset,
|
||||||
|
scheme,
|
||||||
|
nullPreserving,
|
||||||
|
printKey,
|
||||||
|
outputHex,
|
||||||
|
rawCrib
|
||||||
|
] = args,
|
||||||
|
crib = rawCrib.toLowerCase(),
|
||||||
|
output = [];
|
||||||
let result,
|
let result,
|
||||||
resultUtf8,
|
resultUtf8,
|
||||||
record = "";
|
record = "";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue