Skip to content

Commit 7a951d8

Browse files
committed
Tidied up Chi Square operation
1 parent d9dfaec commit 7a951d8

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/core/config/modules/Default.js

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

src/core/operations/Entropy.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ const Entropy = {
135135
},
136136

137137

138+
/**
139+
* Chi Square operation.
140+
*
141+
* @param {byteArray} data
142+
* @param {Object[]} args
143+
* @returns {number}
144+
*/
145+
runChiSq: function(input, args) {
146+
let distArray = new Array(256).fill(0),
147+
total = 0;
148+
149+
for (let i = 0; i < input.length; i++) {
150+
distArray[input[i]]++;
151+
}
152+
153+
for (let i = 0; i < distArray.length; i++) {
154+
if (distArray[i] > 0) {
155+
total += Math.pow(distArray[i] - input.length / 256, 2) / (input.length / 256);
156+
}
157+
}
158+
159+
return total;
160+
},
161+
162+
138163
/**
139164
* Calculates the Shannon entropy for a given chunk of data.
140165
*
@@ -163,28 +188,6 @@ const Entropy = {
163188
return -entropy;
164189
},
165190

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-
},
188191
};
189192

190193
export default Entropy;

0 commit comments

Comments
 (0)