mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-09 07:45:00 -04:00
Adding basic arithmetic operation
This commit is contained in:
parent
ae1b12c120
commit
f9cbb6246b
6 changed files with 157 additions and 1 deletions
|
@ -171,7 +171,8 @@
|
|||
"Bit shift right",
|
||||
"Rotate left",
|
||||
"Rotate right",
|
||||
"ROT13"
|
||||
"ROT13",
|
||||
"Add/Subtract/Multiply/Divide by value"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -35,6 +35,31 @@ export function createNumArray(input, delim) {
|
|||
return numbers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts two strings to a number array.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {string} userValue
|
||||
* @returns {BigNumber[]}
|
||||
*/
|
||||
export function createNumArrayFromTwoStrings(input, userValue) {
|
||||
const numbers = [];
|
||||
let num;
|
||||
try {
|
||||
num = BigNumber(input.trim());
|
||||
if (!num.isNaN()) {
|
||||
numbers.push(num);
|
||||
}
|
||||
num = BigNumber(userValue.trim());
|
||||
if (!num.isNaN()) {
|
||||
numbers.push(num);
|
||||
}
|
||||
} catch (err) {
|
||||
// This line is not a valid number
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds an array of numbers and returns the value.
|
||||
|
|
|
@ -46,6 +46,11 @@ export const HASH_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma"];
|
|||
*/
|
||||
export const IP_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma", "Semi-colon"];
|
||||
|
||||
/**
|
||||
* Math operations
|
||||
*/
|
||||
export const ARITMETIC_OPTIONS = ["Add", "Subtract", "Multiply", "Divide"];
|
||||
|
||||
/**
|
||||
* Split delimiters.
|
||||
*/
|
||||
|
|
74
src/core/operations/AddSubMulDivValue.mjs
Normal file
74
src/core/operations/AddSubMulDivValue.mjs
Normal file
|
@ -0,0 +1,74 @@
|
|||
/**
|
||||
* @author BigYellowHammer
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation.mjs";
|
||||
import { sum, sub, multi, div, createNumArrayFromTwoStrings } from "../lib/Arithmetic.mjs";
|
||||
import { ARITMETIC_OPTIONS } from "../lib/Delim.mjs";
|
||||
|
||||
|
||||
/**
|
||||
* Sum operation
|
||||
*/
|
||||
class AddSubMulDivValue extends Operation {
|
||||
|
||||
/**
|
||||
* Sum constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Add/Subtract/Multiply/Divide by value";
|
||||
this.module = "Default";
|
||||
this.description = "Performs aritmetical operation between the input and argument specified by the user";
|
||||
this.inputType = "string";
|
||||
this.outputType = "BigNumber";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Operation",
|
||||
"type": "option",
|
||||
"value": ARITMETIC_OPTIONS,
|
||||
},
|
||||
{
|
||||
"name": "Value",
|
||||
"type": "shortString",
|
||||
"value": ""
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
|
||||
const arr = createNumArrayFromTwoStrings(input, args[1]);
|
||||
let val;
|
||||
|
||||
switch (args[0]) {
|
||||
case "Add":
|
||||
val = sum(arr);
|
||||
break;
|
||||
case "Subtract":
|
||||
val = sub(arr);
|
||||
break;
|
||||
case "Multiply":
|
||||
val = multi(arr);
|
||||
break;
|
||||
case "Divide":
|
||||
val = div(arr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AddSubMulDivValue;
|
|
@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs";
|
|||
import "./tests/JA3Fingerprint.mjs";
|
||||
import "./tests/JA3SFingerprint.mjs";
|
||||
import "./tests/HASSH.mjs";
|
||||
import "./tests/AddSubMulDivValue.mjs";
|
||||
|
||||
|
||||
// Cannot test operations that use the File type yet
|
||||
|
|
50
tests/operations/tests/AddSubMulDivValue.mjs
Normal file
50
tests/operations/tests/AddSubMulDivValue.mjs
Normal file
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* BitwiseOp tests
|
||||
*
|
||||
* @author BigYellowHammer
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "Add value",
|
||||
input: "9",
|
||||
expectedOutput: "11",
|
||||
recipeConfig: [
|
||||
{ "op": "Add/Subtract/Multiply/Divide by value",
|
||||
"args": ["Add", "2"] },
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Subtract value",
|
||||
input: "9",
|
||||
expectedOutput: "7",
|
||||
recipeConfig: [
|
||||
{ "op": "Add/Subtract/Multiply/Divide by value",
|
||||
"args": ["Subtract", "2"] },
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Multiply by value",
|
||||
input: "9",
|
||||
expectedOutput: "18",
|
||||
recipeConfig: [
|
||||
{ "op": "Add/Subtract/Multiply/Divide by value",
|
||||
"args": ["Multiply", "2"] },
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Multiply by value",
|
||||
input: "10",
|
||||
expectedOutput: "5",
|
||||
recipeConfig: [
|
||||
{ "op": "Add/Subtract/Multiply/Divide by value",
|
||||
"args": ["Divide", "2"] },
|
||||
|
||||
]
|
||||
}
|
||||
]);
|
Loading…
Add table
Add a link
Reference in a new issue