Created
August 14, 2021 07:51
-
-
Save bricef/86333b1d602d3876b27bcead3f3cefe4 to your computer and use it in GitHub Desktop.
typescript password 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
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