Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Created March 18, 2021 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nblackburn/1bc9a17322832a52ea5a63ce4a6615b6 to your computer and use it in GitHub Desktop.
Save nblackburn/1bc9a17322832a52ea5a63ce4a6615b6 to your computer and use it in GitHub Desktop.
Password Entropy

Password Entropy

Calculate the entropy of a password.

Usage

const passEnt = require('passEnt');

const H = passEnt('pswd'); // 18.801758872564367
const pools = [
{ id: 'numbers', pattern: /[0-9]/, size: 10 },
{ id: 'l_letters', pattern: /[a-z]/, size: 26 },
{ id: 'u_letters', pattern: /[A-Z]/, size: 26 },
{ id: 'symbols', pattern: /[^a-zA-Z0-9]/, size: 40 }
];
const getUsedPools = (password) => {
const used = [];
for(let index = 0; index < password.length; index++) {
const char = password.charAt(index);
const pool = pools.find(p => p.pattern.test(char));
if (pool && used.findIndex(p => p.id === pool.id) === -1) {
used.push(pool);
}
}
return used;
};
const calcPoolSize = (pools) => {
let size = 0;
pools.forEach(pool => {
size += pool.size;
});
return size;
};
const calcPoolEntropy = (pools) => {
const size = calcPoolSize(pools);
return Math.log2(size);
};
module.exports = (password) => {
const pools = getUsedPools(password);
const charEntropy = calcPoolEntropy(pools);
return password.length * charEntropy;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment