Skip to content

Commit d9dfaec

Browse files
committed
Merge branch 'chi' of https://github.com/bwhitn/CyberChef into bwhitn-chi
2 parents 80719ae + 4ca2a30 commit d9dfaec

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/core/config/Categories.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ const Categories = [
302302
ops: [
303303
"Entropy",
304304
"Frequency distribution",
305+
"Chi Square",
305306
"Detect File Type",
306307
"Scan for Embedded Files",
307308
"Disassemble x86",

src/core/config/OperationConfig.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,13 @@ const OperationConfig = {
32053205
}
32063206
]
32073207
},
3208+
"Chi Square": {
3209+
module: "Default",
3210+
description: "Calculates the Chi Square distribution of values.",
3211+
inputType: "byteArray",
3212+
outputType: "number",
3213+
args: []
3214+
},
32083215
"Numberwang": {
32093216
module: "Default",
32103217
description: "Based on the popular gameshow by Mitchell and Webb.",

src/core/config/modules/Default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ OpModules.Default = {
143143
"Microsoft Script Decoder": MS.runDecodeScript,
144144
"Entropy": Entropy.runEntropy,
145145
"Frequency distribution": Entropy.runFreqDistrib,
146+
"Chi Square": Entropy.calcChiSq,
146147
"Detect File Type": FileType.runDetect,
147148
"Scan for Embedded Files": FileType.runScanForEmbeddedFiles,
148149
"Generate UUID": UUID.runGenerateV4,

src/core/operations/Entropy.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ const Entropy = {
163163
return -entropy;
164164
},
165165

166+
167+
/**
168+
* Calculates the Chi Square distribution of values.
169+
*
170+
* @private
171+
* @param {byteArray} data
172+
* @param {Object[]} args
173+
* @returns {number}
174+
*/
175+
calcChiSq: function(input, args) {
176+
let distArray = new Array(256).fill(0),
177+
total = 0;
178+
for (let i = 0; i < input.length; i++) {
179+
distArray[input[i]]++;
180+
}
181+
for (let i = 0; i < distArray.length; i++) {
182+
if (distArray[i] > 0) {
183+
total += Math.pow(distArray[i] - input.length / 256, 2) / (input.length / 256);
184+
}
185+
}
186+
return total;
187+
},
166188
};
167189

168190
export default Entropy;

0 commit comments

Comments
 (0)