Password Strength By Entropy
Estimate password entropy from character pools, unique characters and common patterns to produce a five-level score.
Code
const COMMON_PATTERNS = [/^123456/, /password/i, /qwerty/i, /(.)\1{2,}/];
// Password strength estimation based on character-pool entropy.
export function estimatePasswordStrength(password) {
const value = String(password);
const pools = [
[/[a-z]/, 26],
[/[A-Z]/, 26],
[/[0-9]/, 10],
[/[^A-Za-z0-9]/, 33],
];
const poolSize = pools.reduce((total, [pattern, size]) => {
return total + (pattern.test(value) ? size : 0);
}, 0);
const unique = new Set(value).size;
const effectiveLength = Math.max(unique, Math.ceil(value.length * 0.75));
let entropyBits = poolSize > 0 ? effectiveLength * Math.log2(poolSize) : 0;
for (const pattern of COMMON_PATTERNS) {
if (pattern.test(value)) entropyBits *= 0.5;
}
if (value.length < 8) entropyBits *= 0.6;
const thresholds = [20, 40, 60, 80];
let score = thresholds.filter((threshold) => entropyBits >= threshold).length;
if (value.length === 0) score = 0;
const labels = ["very weak", "weak", "fair", "strong", "very strong"];
return {
entropyBits: Math.round(entropyBits),
score,
label: labels[score],
poolSize,
length: value.length,
uniqueCharacters: unique,
};
}How it works
Count the function estimatePasswordStrength as the entire surface area of Password Strength By Entropy.
Repetition is confined to for (const pattern of COMMON_PATTERNS) — the rest of the file is straight-line.
The result comes back as an object carrying score, poolSize, length, unique.
Putting it to use
Entry signature: export function estimatePasswordStrength(password)
estimatePasswordStrength is the public entry point; everything else in the file exists to support it.
The call returns score, poolSize, length, unique — destructure what you need at the call site.
Core lines
const poolSize = pools.reduce((total, [pattern, size]) => {
return total + (pattern.test(value) ? size : 0);
}, 0);
let score = thresholds.filter((threshold) => entropyBits >= threshold).length;
const unique = new Set(value).size;
Interactive Tools for This Topic
Password Strength MeterScore a password from 0 to 100 based on length and character classes, and get clear suggestions to make it stronger. Everything runs locally.Password Entropy EstimatorEstimate a password's alphabet size and Shannon entropy in bits, then translate that into offline crack times at realistic guessing rates.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.
FAQ
- Does `estimatePasswordStrength` modify its input?
- No mutation happens — the function reads its input and returns a new value.
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `estimatePasswordStrength` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.