Browse Source

Converted JS operations

Deleted legacy files, neatened args in other ported ops
Matt C 7 years ago
parent
commit
176e83a79f

+ 1 - 3
src/core/operations/BlowfishDecrypt.mjs

@@ -78,9 +78,7 @@ class BlowfishDecrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
+            [,, mode, inputType, outputType] = args;
 
         if (key.length === 0) return "Enter a key";
 

+ 1 - 3
src/core/operations/BlowfishEncrypt.mjs

@@ -75,9 +75,7 @@ class BlowfishEncrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
+            [,, mode, inputType, outputType] = args;
 
         if (key.length === 0) return "Enter a key";
 

+ 1 - 3
src/core/operations/DESDecrypt.mjs

@@ -63,9 +63,7 @@ class DESDecrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
+            [,, mode, inputType, outputType] = args;
 
         if (key.length !== 8) {
             return `Invalid key length: ${key.length} bytes

+ 1 - 3
src/core/operations/DESEncrypt.mjs

@@ -63,9 +63,7 @@ class DESEncrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteArray(args[1].string, args[1].option),
-            mode = args[2],
-            inputType = args[3],
-            outputType = args[4];
+            [,, mode, inputType, outputType] = args;
 
         if (key.length !== 8) {
             return `Invalid key length: ${key.length} bytes

+ 1 - 2
src/core/operations/DeriveEVPKey.mjs

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

+ 1 - 3
src/core/operations/DerivePBKDF2Key.mjs

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

+ 94 - 0
src/core/operations/JavaScriptBeautify.mjs

@@ -0,0 +1,94 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import escodegen from "escodegen";
+import * as esprima from "esprima";
+
+/**
+ * JavaScript Beautify operation
+ */
+class JavaScriptBeautify extends Operation {
+
+    /**
+     * JavaScriptBeautify constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "JavaScript Beautify";
+        this.module = "Code";
+        this.description = "Parses and pretty prints valid JavaScript code. Also works with JavaScript Object Notation (JSON).";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Indent string",
+                "type": "binaryShortString",
+                "value": "\\t"
+            },
+            {
+                "name": "Quotes",
+                "type": "option",
+                "value": ["Auto", "Single", "Double"]
+            },
+            {
+                "name": "Semicolons before closing braces",
+                "type": "boolean",
+                "value": true
+            },
+            {
+                "name": "Include comments",
+                "type": "boolean",
+                "value": true
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const beautifyIndent = args[0] || "\\t",
+            quotes = args[1].toLowerCase(),
+            [,, beautifySemicolons, beautifyComment] = args;
+        let result = "",
+            AST;
+
+        try {
+            AST = esprima.parseScript(input, {
+                range: true,
+                tokens: true,
+                comment: true
+            });
+
+            const options = {
+                format: {
+                    indent: {
+                        style: beautifyIndent
+                    },
+                    quotes: quotes,
+                    semicolons: beautifySemicolons,
+                },
+                comment: beautifyComment
+            };
+
+            if (options.comment)
+                AST = escodegen.attachComments(AST, AST.comments, AST.tokens);
+
+            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;
+        }
+        return result;
+    }
+
+}
+
+export default JavaScriptBeautify;

+ 57 - 0
src/core/operations/JavaScriptMinify.mjs

@@ -0,0 +1,57 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import * as esprima from "esprima";
+import escodegen from "escodegen";
+import esmangle from "esmangle";
+
+/**
+ * JavaScript Minify operation
+ */
+class JavaScriptMinify extends Operation {
+
+    /**
+     * JavaScriptMinify constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "JavaScript Minify";
+        this.module = "Code";
+        this.description = "Compresses JavaScript code.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        let result = "";
+        const AST = esprima.parseScript(input),
+            optimisedAST = esmangle.optimize(AST, null),
+            mangledAST = esmangle.mangle(optimisedAST);
+
+        result = escodegen.generate(mangledAST, {
+            format: {
+                renumber:    true,
+                hexadecimal: true,
+                escapeless:  true,
+                compact:     true,
+                semicolons:  false,
+                parentheses: false
+            }
+        });
+        return result;
+    }
+
+}
+
+export default JavaScriptMinify;

+ 77 - 0
src/core/operations/JavaScriptParser.mjs

@@ -0,0 +1,77 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import * as esprima from "esprima";
+
+/**
+ * JavaScript Parser operation
+ */
+class JavaScriptParser extends Operation {
+
+    /**
+     * JavaScriptParser constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "JavaScript Parser";
+        this.module = "Code";
+        this.description = "Returns an Abstract Syntax Tree for valid JavaScript code.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Location info",
+                "type": "boolean",
+                "value": false
+            },
+            {
+                "name": "Range info",
+                "type": "boolean",
+                "value": false
+            },
+            {
+                "name": "Include tokens array",
+                "type": "boolean",
+                "value": false
+            },
+            {
+                "name": "Include comments array",
+                "type": "boolean",
+                "value": false
+            },
+            {
+                "name": "Report errors and try to continue",
+                "type": "boolean",
+                "value": false
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const [parseLoc, parseRange, parseTokens, parseComment, parseTolerant] = args,
+            options = {
+                loc:      parseLoc,
+                range:    parseRange,
+                tokens:   parseTokens,
+                comment:  parseComment,
+                tolerant: parseTolerant
+            };
+        let result = {};
+
+        result = esprima.parseScript(input, options);
+        return JSON.stringify(result, null, 2);
+    }
+
+}
+
+export default JavaScriptParser;

+ 1 - 2
src/core/operations/RC2Decrypt.mjs

@@ -58,8 +58,7 @@ class RC2Decrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteString(args[1].string, args[1].option),
-            inputType = args[2],
-            outputType = args[3],
+            [,, inputType, outputType] = args,
             decipher = forge.rc2.createDecryptionCipher(key);
 
         input = Utils.convertToByteString(input, inputType);

+ 1 - 2
src/core/operations/RC2Encrypt.mjs

@@ -59,8 +59,7 @@ class RC2Encrypt extends Operation {
     run(input, args) {
         const key = Utils.convertToByteString(args[0].string, args[0].option),
             iv = Utils.convertToByteString(args[1].string, args[1].option),
-            inputType = args[2],
-            outputType = args[3],
+            [,, inputType, outputType] = args,
             cipher = forge.rc2.createEncryptionCipher(key);
 
         input = Utils.convertToByteString(input, inputType);

+ 0 - 369
src/core/operations/legacy/BitwiseOp.js

@@ -1,369 +0,0 @@
-import Utils from "../Utils.js";
-import {toHex} from "../lib/Hex";
-
-
-/**
- * Bitwise operations.
- *
- * @author n1474335 [n1474335@gmail.com]
- * @copyright Crown Copyright 2016
- * @license Apache-2.0
- *
- * @namespace
- */
-const BitwiseOp = {
-
-    /**
-     * Runs bitwise operations across the input data.
-     *
-     * @private
-     * @param {byteArray} input
-     * @param {byteArray} key
-     * @param {function} func - The bitwise calculation to carry out
-     * @param {boolean} nullPreserving
-     * @param {string} scheme
-     * @returns {byteArray}
-     */
-    _bitOp: function (input, key, func, nullPreserving, scheme) {
-        if (!key || !key.length) key = [0];
-        let result = [],
-            x = null,
-            k = null,
-            o = null;
-
-        for (let i = 0; i < input.length; i++) {
-            k = key[i % key.length];
-            o = input[i];
-            x = nullPreserving && (o === 0 || o === k) ? o : func(o, k);
-            result.push(x);
-            if (scheme &&
-                scheme !== "Standard" &&
-                !(nullPreserving && (o === 0 || o === k))) {
-                switch (scheme) {
-                    case "Input differential":
-                        key[i % key.length] = x;
-                        break;
-                    case "Output differential":
-                        key[i % key.length] = o;
-                        break;
-                }
-            }
-        }
-
-        return result;
-    },
-
-
-    /**
-     * @constant
-     * @default
-     */
-    XOR_PRESERVE_NULLS: false,
-    /**
-     * @constant
-     * @default
-     */
-    XOR_SCHEME: ["Standard", "Input differential", "Output differential"],
-    /**
-     * @constant
-     * @default
-     */
-    KEY_FORMAT: ["Hex", "Base64", "UTF8", "Latin1"],
-
-    /**
-     * XOR operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runXor: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string || "", args[0].option),
-            scheme = args[1],
-            nullPreserving = args[2];
-
-        return BitwiseOp._bitOp(input, key, BitwiseOp._xor, nullPreserving, scheme);
-    },
-
-
-    /**
-     * @constant
-     * @default
-     */
-    XOR_BRUTE_KEY_LENGTH: 1,
-    /**
-     * @constant
-     * @default
-     */
-    XOR_BRUTE_SAMPLE_LENGTH: 100,
-    /**
-     * @constant
-     * @default
-     */
-    XOR_BRUTE_SAMPLE_OFFSET: 0,
-    /**
-     * @constant
-     * @default
-     */
-    XOR_BRUTE_PRINT_KEY: true,
-    /**
-     * @constant
-     * @default
-     */
-    XOR_BRUTE_OUTPUT_HEX: false,
-
-    /**
-     * XOR Brute Force operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runXorBrute: function (input, args) {
-        const keyLength = args[0],
-            sampleLength = args[1],
-            sampleOffset = args[2],
-            scheme = args[3],
-            nullPreserving = args[4],
-            printKey = args[5],
-            outputHex = args[6],
-            crib = args[7].toLowerCase();
-
-        let output = [],
-            result,
-            resultUtf8,
-            record = "";
-
-        input = input.slice(sampleOffset, sampleOffset + sampleLength);
-
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
-
-        /**
-         * Converts an integer to an array of bytes expressing that number.
-         *
-         * @param {number} int
-         * @param {number} len - Length of the resulting array
-         * @returns {array}
-         */
-        const intToByteArray = (int, len) => {
-            let res = Array(len).fill(0);
-            for (let i = len - 1; i >= 0; i--) {
-                res[i] = int & 0xff;
-                int = int >>> 8;
-            }
-            return res;
-        };
-
-        for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
-            if (key % 10000 === 0 && ENVIRONMENT_IS_WORKER()) {
-                self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
-            }
-
-            result = BitwiseOp._bitOp(input, intToByteArray(key, keyLength), BitwiseOp._xor, nullPreserving, scheme);
-            resultUtf8 = Utils.byteArrayToUtf8(result);
-            record = "";
-
-            if (crib && resultUtf8.toLowerCase().indexOf(crib) < 0) continue;
-            if (printKey) record += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
-            if (outputHex) {
-                record += toHex(result);
-            } else {
-                record += Utils.printable(resultUtf8, false);
-            }
-
-            output.push(record);
-        }
-
-        return output.join("\n");
-    },
-
-
-    /**
-     * NOT operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runNot: function (input, args) {
-        return BitwiseOp._bitOp(input, null, BitwiseOp._not);
-    },
-
-
-    /**
-     * AND operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runAnd: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string || "", args[0].option);
-
-        return BitwiseOp._bitOp(input, key, BitwiseOp._and);
-    },
-
-
-    /**
-     * OR operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runOr: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string || "", args[0].option);
-
-        return BitwiseOp._bitOp(input, key, BitwiseOp._or);
-    },
-
-
-    /**
-     * ADD operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runAdd: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string || "", args[0].option);
-
-        return BitwiseOp._bitOp(input, key, BitwiseOp._add);
-    },
-
-
-    /**
-     * SUB operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runSub: function (input, args) {
-        const key = Utils.convertToByteArray(args[0].string || "", args[0].option);
-
-        return BitwiseOp._bitOp(input, key, BitwiseOp._sub);
-    },
-
-
-    /**
-     * Bit shift left operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runBitShiftLeft: function(input, args) {
-        const amount = args[0];
-
-        return input.map(b => {
-            return (b << amount) & 0xff;
-        });
-    },
-
-
-    /**
-     * @constant
-     * @default
-     */
-    BIT_SHIFT_TYPE: ["Logical shift", "Arithmetic shift"],
-
-    /**
-     * Bit shift right operation.
-     *
-     * @param {byteArray} input
-     * @param {Object[]} args
-     * @returns {byteArray}
-     */
-    runBitShiftRight: function(input, args) {
-        const amount = args[0],
-            type = args[1],
-            mask = type === "Logical shift" ? 0 : 0x80;
-
-        return input.map(b => {
-            return (b >>> amount) ^ (b & mask);
-        });
-    },
-
-
-    /**
-     * XOR bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @param {number} key
-     * @returns {number}
-     */
-    _xor: function (operand, key) {
-        return operand ^ key;
-    },
-
-
-    /**
-     * NOT bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @returns {number}
-     */
-    _not: function (operand, _) {
-        return ~operand & 0xff;
-    },
-
-
-    /**
-     * AND bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @param {number} key
-     * @returns {number}
-     */
-    _and: function (operand, key) {
-        return operand & key;
-    },
-
-
-    /**
-     * OR bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @param {number} key
-     * @returns {number}
-     */
-    _or: function (operand, key) {
-        return operand | key;
-    },
-
-
-    /**
-     * ADD bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @param {number} key
-     * @returns {number}
-     */
-    _add: function (operand, key) {
-        return (operand + key) % 256;
-    },
-
-
-    /**
-     * SUB bitwise calculation.
-     *
-     * @private
-     * @param {number} operand
-     * @param {number} key
-     * @returns {number}
-     */
-    _sub: function (operand, key) {
-        const result = operand - key;
-        return (result < 0) ? 256 + result : result;
-    },
-
-};
-
-export default BitwiseOp;

+ 0 - 164
src/core/operations/legacy/JS.js

@@ -1,164 +0,0 @@
-import * as esprima from "esprima";
-import escodegen from "escodegen";
-import esmangle from "esmangle";
-
-
-/**
- * JavaScript operations.
- *
- * @author n1474335 [n1474335@gmail.com]
- * @copyright Crown Copyright 2016
- * @license Apache-2.0
- *
- * @namespace
- */
-const JS = {
-
-    /**
-     * @constant
-     * @default
-     */
-    PARSE_LOC: false,
-    /**
-     * @constant
-     * @default
-     */
-    PARSE_RANGE: false,
-    /**
-     * @constant
-     * @default
-     */
-    PARSE_TOKENS: false,
-    /**
-     * @constant
-     * @default
-     */
-    PARSE_COMMENT: false,
-    /**
-     * @constant
-     * @default
-     */
-    PARSE_TOLERANT: false,
-
-    /**
-     * JavaScript Parser operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runParse: function (input, args) {
-        let parseLoc = args[0],
-            parseRange = args[1],
-            parseTokens = args[2],
-            parseComment = args[3],
-            parseTolerant = args[4],
-            result = {},
-            options = {
-                loc:      parseLoc,
-                range:    parseRange,
-                tokens:   parseTokens,
-                comment:  parseComment,
-                tolerant: parseTolerant
-            };
-
-        result = esprima.parseScript(input, options);
-        return JSON.stringify(result, null, 2);
-    },
-
-
-    /**
-     * @constant
-     * @default
-     */
-    BEAUTIFY_INDENT: "\\t",
-    /**
-     * @constant
-     * @default
-     */
-    BEAUTIFY_QUOTES: ["Auto", "Single", "Double"],
-    /**
-     * @constant
-     * @default
-     */
-    BEAUTIFY_SEMICOLONS: true,
-    /**
-     * @constant
-     * @default
-     */
-    BEAUTIFY_COMMENT: true,
-
-    /**
-     * JavaScript Beautify operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runBeautify: function(input, args) {
-        let beautifyIndent = args[0] || JS.BEAUTIFY_INDENT,
-            quotes = args[1].toLowerCase(),
-            beautifySemicolons = args[2],
-            beautifyComment = args[3],
-            result = "",
-            AST;
-
-        try {
-            AST = esprima.parseScript(input, {
-                range: true,
-                tokens: true,
-                comment: true
-            });
-
-            const options = {
-                format: {
-                    indent: {
-                        style: beautifyIndent
-                    },
-                    quotes: quotes,
-                    semicolons: beautifySemicolons,
-                },
-                comment: beautifyComment
-            };
-
-            if (options.comment)
-                AST = escodegen.attachComments(AST, AST.comments, AST.tokens);
-
-            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;
-        }
-        return result;
-    },
-
-
-    /**
-     * JavaScript Minify operation.
-     *
-     * @param {string} input
-     * @param {Object[]} args
-     * @returns {string}
-     */
-    runMinify: function(input, args) {
-        let result = "",
-            AST = esprima.parseScript(input),
-            optimisedAST = esmangle.optimize(AST, null),
-            mangledAST = esmangle.mangle(optimisedAST);
-
-        result = escodegen.generate(mangledAST, {
-            format: {
-                renumber:    true,
-                hexadecimal: true,
-                escapeless:  true,
-                compact:     true,
-                semicolons:  false,
-                parentheses: false
-            }
-        });
-        return result;
-    },
-
-};
-
-export default JS;