Skip to content

Instantly share code, notes, and snippets.

@bricef
Created August 14, 2021 07:51
Show Gist options
  • Save bricef/86333b1d602d3876b27bcead3f3cefe4 to your computer and use it in GitHub Desktop.
Save bricef/86333b1d602d3876b27bcead3f3cefe4 to your computer and use it in GitHub Desktop.
typescript password entropy
const classes : Array<[RegExp, number]>= [
[ /^[0-9]+$/, 3.322],
[ /^[0-9A-F]+$/, 4.000],
[ /^([A-Z]+|[a-z]+)$/, 4.700],
[ /^([A-Z0-9]+|[a-z0-9]+)$/, 5.170],
[ /^[A-Za-z]+$/, 5.700],
[ /^[A-Za-z0-9]+$/, 5.954],
[ /^[a-z0-9!"#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]*$/i, 6.555],
[ /^[\p{Cc}\p{Cn}\p{Cs}]+$/gu, 7.768],
[ /^.+$/, 8.000]
]
function bitsPerSymbolGivenString(p: string): number{
// See https://en.wikipedia.org/wiki/Password_strength
for (let [pattern, bps] of classes){
if(pattern.test(p)){
console.log("bps", bps)
return bps
}
}
return 0
}
export function passwordEntropyBits(p: string): number{
if (p == "") {return 0}
return bitsPerSymbolGivenString(p) * p.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment