mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 08:16:17 -04:00
Magic operation now calculates the entropy of each option and displays tooltips explaining the properties.
This commit is contained in:
parent
559741fd07
commit
56d33ea487
2 changed files with 50 additions and 10 deletions
|
@ -173,6 +173,24 @@ class Magic {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the Shannon entropy of the input data.
|
||||
*
|
||||
* @returns {number}
|
||||
*/
|
||||
calcEntropy() {
|
||||
let prob = this._freqDist(),
|
||||
entropy = 0,
|
||||
p;
|
||||
|
||||
for (let i = 0; i < prob.length; i++) {
|
||||
p = prob[i] / 100;
|
||||
if (p === 0) continue;
|
||||
entropy += p * Math.log(p) / Math.log(2);
|
||||
}
|
||||
return -entropy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate various simple brute-forced encodings of the data (trucated to 100 bytes).
|
||||
*
|
||||
|
@ -262,6 +280,7 @@ class Magic {
|
|||
languageScores: this.detectLanguage(extLang),
|
||||
fileType: this.detectFileType(),
|
||||
isUTF8: this.isUTF8(),
|
||||
entropy: this.calcEntropy(),
|
||||
matchingOps: matchingOps,
|
||||
useful: useful
|
||||
});
|
||||
|
@ -324,6 +343,10 @@ class Magic {
|
|||
aScore += a.recipe.length;
|
||||
bScore += b.recipe.length;
|
||||
|
||||
// Lower entropy is "better", so we add the entropy to the score
|
||||
aScore += a.entropy;
|
||||
bScore += b.entropy;
|
||||
|
||||
return aScore - bScore;
|
||||
});
|
||||
}
|
||||
|
@ -351,12 +374,14 @@ class Magic {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calculates the number of times each byte appears in the input
|
||||
* Calculates the number of times each byte appears in the input as a percentage
|
||||
*
|
||||
* @private
|
||||
* @returns {number[]}
|
||||
*/
|
||||
_freqDist() {
|
||||
if (this.freqDist) return this.freqDist;
|
||||
|
||||
const len = this.inputBuffer.length;
|
||||
let i = len,
|
||||
counts = new Array(256).fill(0);
|
||||
|
@ -367,9 +392,10 @@ class Magic {
|
|||
counts[this.inputBuffer[i]]++;
|
||||
}
|
||||
|
||||
return counts.map(c => {
|
||||
this.freqDist = counts.map(c => {
|
||||
return c / len * 100;
|
||||
});
|
||||
return this.freqDist;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue