Tidied up Chi Square operation

This commit is contained in:
n1474335 2017-12-19 15:02:01 +00:00
parent d9dfaec84c
commit 7a951d86d8
2 changed files with 26 additions and 23 deletions

View File

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

View File

@ -135,6 +135,31 @@ const Entropy = {
},
/**
* Chi Square operation.
*
* @param {byteArray} data
* @param {Object[]} args
* @returns {number}
*/
runChiSq: 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;
},
/**
* Calculates the Shannon entropy for a given chunk of data.
*
@ -163,28 +188,6 @@ 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;