瀏覽代碼

Merge branch 'chi' of https://github.com/bwhitn/CyberChef into bwhitn-chi

n1474335 7 年之前
父節點
當前提交
d9dfaec84c

+ 1 - 0
src/core/config/Categories.js

@@ -302,6 +302,7 @@ const Categories = [
         ops: [
             "Entropy",
             "Frequency distribution",
+            "Chi Square",
             "Detect File Type",
             "Scan for Embedded Files",
             "Disassemble x86",

+ 7 - 0
src/core/config/OperationConfig.js

@@ -3205,6 +3205,13 @@ const OperationConfig = {
             }
         ]
     },
+    "Chi Square": {
+        module: "Default",
+        description: "Calculates the Chi Square distribution of values.",
+        inputType: "byteArray",
+        outputType: "number",
+        args: []
+    },
     "Numberwang": {
         module: "Default",
         description: "Based on the popular gameshow by Mitchell and Webb.",

+ 1 - 0
src/core/config/modules/Default.js

@@ -143,6 +143,7 @@ OpModules.Default = {
     "Microsoft Script Decoder": MS.runDecodeScript,
     "Entropy":              Entropy.runEntropy,
     "Frequency distribution": Entropy.runFreqDistrib,
+    "Chi Square":           Entropy.calcChiSq,
     "Detect File Type":     FileType.runDetect,
     "Scan for Embedded Files": FileType.runScanForEmbeddedFiles,
     "Generate UUID":        UUID.runGenerateV4,

+ 22 - 0
src/core/operations/Entropy.js

@@ -163,6 +163,28 @@ const Entropy = {
         return -entropy;
     },
 
+
+    /**
+     * Calculates the Chi Square distribution of values.
+     *
+     * @private
+     * @param {byteArray} data
+     * @param {Object[]} args
+     * @returns {number}
+     */
+    calcChiSq: function(input, args) {
+        let distArray = new Array(256).fill(0),
+            total = 0;
+        for (let i = 0; i < input.length; i++) {
+            distArray[input[i]]++;
+        }
+        for (let i = 0; i < distArray.length; i++) {
+            if (distArray[i] > 0) {
+                total += Math.pow(distArray[i] - input.length / 256, 2) / (input.length / 256);
+            }
+        }
+        return total;
+    },
 };
 
 export default Entropy;