Created
December 22, 2018 19:33
-
-
Save ancms2600/ecd0b1ad6ef84cecf08858fa9956ebba to your computer and use it in GitHub Desktop.
Score and sort a list of hex strings by most entropy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sort a list of hexadecimal strings | |
* by the most unique bytes per line (ie. most entropy). | |
* Useful when searching a list for RNG values, such as cryptographic keys. | |
*/ | |
const fs = require('fs'); | |
const [, , file] = process.argv; | |
const content = fs.readFileSync(file); | |
const lines = content.toString().split(/\n/g); | |
const score = s => { | |
let idx = {}; | |
let score = s.length/2; | |
for (let i=0; i<s.length; i+=2) { | |
const hex = s.substr(i,2); | |
if (null != idx[hex]) score--; | |
idx[hex] = true; | |
} | |
return score; | |
}; | |
const stats = []; | |
for (const line of lines) { | |
if ('' == line) continue; | |
stats.push({ line: line, score: score(line) }); | |
} | |
stats.sort((a, b) => { | |
if (a.score < b.score) return +1 | |
else if (a.score > b.score) return -1; | |
else return 0; | |
}); | |
for (const line of stats) { | |
// console.log(line); | |
console.log(`${line.line} ${line.score}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment