Strength
NIST-style password policy with @virastack/password/strength: length, common passwords, repeats and personal info.
definePolicy()
Define the policy once in a shared file without "use client". The form and the server import the same object.
// lib/password-policy.ts
// Shared by the form and the server, so no "use client".
import { definePolicy } from "@virastack/password/strength";
export const policy = definePolicy({ preset: "nist", blocklist: ["virastack"] });Presets
| Preset | Length | Composition |
|---|---|---|
nist (default) | 15–64 | None. Per NIST SP 800-63B-4: length and a blocklist, no composition rules |
mfa | 8–64 | None. For a password used with a second factor (NIST SP 800-63B-4) |
legacy (deprecated) | 8–64 | Lowercase, uppercase, digit and symbol |
Every preset value can be changed:
definePolicy({ preset: "mfa" }) // 8 characters, with a second factor
definePolicy({ require: { digit: true } }) // composition rules, if you must
definePolicy({ blocklist: ["acme", "acme pay"] }) // the whole password must not be your product
definePolicy({ normalization: "NFC" }) // NIST's recommendation; NFKC by default
definePolicy({ blockCommon: false, blockRepetitive: false })Conflicting rules (for example minLength above maxLength) throw when the policy is defined.
Result
import { evaluate } from "@virastack/password/strength"
import { policy } from "@/lib/password-policy"
evaluate("correct horse battery staple", policy)
// {
// score: 0, // 0–1 fail the policy, 2–4 pass
// level: "weak", // "weak" | "fair" | "good" | "strong"
// length: 28, // code points after normalization
// valid: false,
// checks: { minLength: true, maxLength: true, notCommon: false, ... },
// issues: ["password.common"],
// }| Key | When |
|---|---|
password.tooShort / tooLong | Outside minLength–maxLength |
password.noLowercase / noUppercase / noDigit / noSymbol | A required class is missing (legacy) |
password.common | A common password, also with digits, symbols or leetspeak around it |
password.blocked | Is a word from the policy blocklist, with digits or symbols around it |
password.repetitive | Repeats or sequences: aaaa…, abcabc…, 12345…, qwerty… |
password.containsPersonalInfo | Is a forbidden value or a part of it, with digits or symbols around it |
password.pwned | Found in a breach (validatePassword with pwned, server only) |
A valid password is fair at the minimum length, good from 5 characters more and strong from 10 more. An uppercase letter, a digit or a symbol doesn't change the level: NIST drops composition rules, so the meter doesn't bring them back. Repeated characters add little. It is not a crack-time estimate.
Why is a complex 14-character password weak? The score follows the policy, not a universal rule. The default nist preset asks for 15 characters, so anything shorter fails it and scores 0–1, however mixed it is. From 15 characters the score rises with length. Need shorter passwords? Use the mfa preset: NIST allows 8 characters when the password is used with a second factor.
In the hook
Pass policy and read strength, issues and strengthProps. The hook doesn't import the engine; it comes with the policy, so a toggle-only page doesn't download it.
15 characters or more
forbidden
The package doesn't know your user; pass the values. As NIST asks, the whole password is compared, not searched for words: it fails when it is a value or a part of it with digits, symbols, leetspeak or repetition around it. Case and accents are ignored.
evaluate(password, policy, { forbidden: [email, name, username] })
// "hello@virastack.com" → "hello" (domain ignored)
// "Vira Stack" → "virastack", "vira", "stack"
// The whole password is compared, after digits, symbols, leetspeak and repetition are removed:
evaluate("vira-1990-2024!", policy, { forbidden: ["Vira Stack"] }) // containsPersonalInfo
evaluate("v1ra-v1ra-v1ra-2024", policy, { forbidden: ["Vira Stack"] }) // containsPersonalInfo
evaluate("viral-garden-path", policy, { forbidden: ["Vira Stack"] }) // passesA short name inside an ordinary word is not a match: with the name Ali, kaliteli-sabah-kahvesi passes and ali1990 fails. Parts shorter than minTokenLength (default 3) are ignored.
Try vira-1990-2024, then viral-garden-path
Unicode
- The password is normalized with NFKC by default, so the same text typed on different keyboards, full-width letters included, gives the same result.
normalization: "NFC"follows NIST's recommendation and keeps full-width letters and ligatures as they are. Switching changes the normalized password you hash, so choose before you have users. - Length counts code points: an emoji or a Turkish letter counts once.
- Zero-width characters don't count towards the length.
- Nothing is cut at
maxLength; a longer password getspassword.tooLong.
Paste
Don't block pasting into password fields. Password managers depend on it, and NIST recommends allowing it. The hook never blocks paste.