mirror of
https://github.com/gchq/CyberChef
synced 2026-01-06 10:33:18 +00:00
Tidied up Chi Square operation
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user