TExcelTAD
Home › Programming for Security: Python, Bash, PowerShell & C Tools › Project: Password-Strength Estimator

Project: Password-Strength Estimator

"Your password must contain an upper-case letter, a digit and a symbol" produces Summer2025!, exactly the kind of password that spraying lists are built from. Composition rules measure the wrong thing: they count character classes, while attackers count guesses. A useful strength meter estimates how many guesses a realistic attacker needs, by recognising the patterns people actually use: dictionary words, capitalised first letters, leetspeak, years and keyboard walks. This project builds a small estimator with three parts (a character-set model, a blocklist and pattern models), compares their answers, and turns the estimate into a time with the lab. The policy side, how passwords are stored and attacked, is in Brute Force and Password Spraying and Applied Cryptography.

Entropy as a guess count

If a password is chosen uniformly at random from CC symbols with length LL, there are CLC^L equally likely candidates, and its entropy in bits is

H=log⁡2(CL)=Llog⁡2CH = \log_2\left(C^L\right) = L \log_2 C

An attacker searching the space finds it after half of it on average, so with RR guesses per second the expected time is

T=2H−1RT = \frac{2^{H-1}}{R}

Ten random characters from the 95 printable ASCII symbols give H=10log⁡295≈65.7H = 10 \log_2 95 \approx 65.7 bits. The catch is the word random. People do not choose uniformly, so the formula applied to Summer2025! (11 characters, four classes) gives about 72 bits, while an attacker who tries common words with a capital letter, a year and a symbol needs only about a hundred million guesses. A realistic estimator therefore computes the guess count under several attacker models and keeps the smallest.

Try the effect of length, character set, hash function and attacker on the time to guess, for genuinely random passwords:

10 random characters from 62 a–z A–Z 0–9entropy H = L · log2 C = 10 · 5.954 = 59.5 bitssearch space C^L = 62^10 = 8.39 × 10¹⁷ guessesguess rate R ≈ 10¹⁰ /s (offline, one GPU, SHA-256 (fast))expected time C^L / (2R) ≈ 1.3 yearsrates: illustrativeorders of magnitude,not benchmarkssecondhourdayyearcenturyuniverse age10⁻³ s10³ s10⁹ s10¹⁵ s10²¹ s14812162024expected time (log scale)SHA-256PBKDF2 600kbcrypt 12L = 10: 1.3 yearslength L →

The lab's rates are illustrative orders of magnitude, not benchmarks. Notice how much the hash function matters: the same password that falls in a year against fast SHA-256 survives far longer against bcrypt, which is why storage and strength are two halves of the same defence.

The estimator

The models below are deliberately small and visible. The word lists stand in for real ones: a production estimator such as zxcvbn ships tens of thousands of common passwords, names and words.

Model Guess count it assumes Bits
charset every string of length LL over the classes used Llog⁡2CL \log_2 C
blocklist the attacker tries known breached passwords first 0 (reject outright)
dictionary + mangling a word from a 10,000-entry list, maybe capitalised, maybe leetspeak, then digits or a year, then symbols log⁡210000+1+1+log⁡2140+…\log_2 10000 + 1 + 1 + \log_2 140 + \ldots
sequence / repeat qwertyuiop, abcdef, aaaa: a short list of patterns a few bits
passphrase kk words from a 7,776-word Diceware list klog⁡27776k \log_2 7776
python · try it yourself
import math, re

BLOCKLIST = {"password", "123456", "12345678", "qwerty", "letmein", "iloveyou", "admin", "welcome"}
COMMON_WORDS = {"password", "dragon", "monkey", "summer", "winter", "football", "welcome", "shadow", "admin", "sunshine"}
DICT_SIZE = 10_000          # model: base words come from a 10,000-word list of common passwords
DICEWARE = {"correct", "horse", "battery", "staple", "lamp", "river", "orbit", "velvet"}   # sample of a 7,776-word list
LEET = str.maketrans("@4310$5!7", "aaeiossit")
SEQS = ["abcdefghijklmnopqrstuvwxyz", "0123456789", "qwertyuiop", "asdfghjkl", "zxcvbnm"]

def charset_bits(pw):
    c = 0
    c += 26 if re.search(r"[a-z]", pw) else 0
    c += 26 if re.search(r"[A-Z]", pw) else 0
    c += 10 if re.search(r"\d", pw) else 0
    c += 33 if re.search(r"[^A-Za-z0-9]", pw) else 0
    return len(pw) * math.log2(c) if c else 0.0

def pattern_bits(pw):
    """Smallest guess count (as bits) under a few attacker models; None if no model applies."""
    s = pw.lower()
    cands = []
    if len(set(pw)) == 1:
        cands.append(("repeat", math.log2(95 * len(pw))))
    for seq in SEQS:
        for src in (seq, seq[::-1]):
            if len(s) >= 3 and s in src:
                cands.append(("sequence", math.log2(len(src) * len(s))))
    words = s.split()
    if len(words) >= 2 and all(w in DICEWARE for w in words):
        cands.append(("passphrase", len(words) * math.log2(7776)))
    m = re.fullmatch(r"([a-z@4310$5!7]+?)(\d*)([^a-z0-9]*)", s)
    if m and m[1].translate(LEET) in COMMON_WORDS:
        digits, symbols = m[2], m[3]
        bits = math.log2(DICT_SIZE)
        bits += 1 if pw[0].isupper() else 0
        bits += 1 if m[1] != m[1].translate(LEET) else 0
        if len(digits) == 4 and 1900 <= int(digits) <= 2039:
            bits += math.log2(140)
        else:
            bits += len(digits) * math.log2(10)
        bits += len(symbols) * math.log2(33)
        cands.append(("dictionary+mangling", bits))
    return min(cands, key=lambda c: c[1]) if cands else None

def estimate(pw):
    if pw.lower() in BLOCKLIST:
        return 0.0, "blocklisted"
    naive = charset_bits(pw)
    pat = pattern_bits(pw)
    if pat and pat[1] < naive:
        return pat[1], pat[0]
    return naive, "charset (assumes uniform choice)"

for pw in ["password", "Summer2025!", "P@ssw0rd", "qwertyuiop", "aaaaaaaaaaaa",
           "correct horse battery staple", "tR7#qL9vX2", "k9vX!2mQ#pL8zR4w"]:
    bits, why = estimate(pw)
    verdict = "reject" if bits < 40 else "weak" if bits < 60 else "ok"
    print(f"{pw!r:32} naive {charset_bits(pw):6.1f}  estimate {bits:6.1f} bits  {verdict:<6} ({why})")

R = 1e10                      # illustrative: one GPU against unsalted SHA-256
for bits in (26.5, 51.7, 65.7):
    print(f"{bits:5.1f} bits at 1e10 guesses/s: {2 ** (bits - 1) / R:.3g} s on average")
'password'                       naive   37.6  estimate    0.0 bits  reject (blocklisted)
'Summer2025!'                    naive   72.3  estimate   26.5 bits  reject (dictionary+mangling)
'P@ssw0rd'                       naive   52.6  estimate   15.3 bits  reject (dictionary+mangling)
'qwertyuiop'                     naive   47.0  estimate    6.6 bits  reject (sequence)
'aaaaaaaaaaaa'                   naive   56.4  estimate   10.2 bits  reject (repeat)
'correct horse battery staple'   naive  164.7  estimate   51.7 bits  weak   (passphrase)
'tR7#qL9vX2'                     naive   65.7  estimate   65.7 bits  ok     (charset (assumes uniform choice))
'k9vX!2mQ#pL8zR4w'               naive  105.1  estimate  105.1 bits  ok     (charset (assumes uniform choice))
 26.5 bits at 1e10 guesses/s: 0.00475 s on average
 51.7 bits at 1e10 guesses/s: 1.83e+05 s on average
 65.7 bits at 1e10 guesses/s: 3e+09 s on average

The naive charset numbers are the "composition rule" view; the estimates are the attacker's view. Summer2025! drops from 72 bits to about 26.5: log⁡210000≈13.3\log_2 10000 \approx 13.3 for the word, 1 bit for the capital, log⁡2140≈7.1\log_2 140 \approx 7.1 for a year between 1900 and 2039 and log⁡233≈5.0\log_2 33 \approx 5.0 for one symbol. At ten billion guesses a second that is a few milliseconds. The four-word passphrase is rated on the passphrase model, 4log⁡27776≈51.74 \log_2 7776 \approx 51.7 bits, far below its naive 164.7 bits but still about 1.8×1051.8 \times 10^5 seconds (two days) on average at the same rate, and tens of thousands of years at the lab's bcrypt rate; five or six words are better. The two random strings get the full charset estimate because no pattern explains them: that is the only case where the formula is honest.

How weak passwords are detected

  • At the moment of choice: the estimator runs when a password is set or changed, and the blocklist check uses a large breached-password corpus (the k-anonymity range query from Brute Force and Password Spraying keeps the password private).
  • In audits of your own systems: check that the policy is enforced in code (minimum length, blocklist), and look for shared or default passwords in configuration files and scripts with secret scanners. Testing users' stored hashes against wordlists is a formal, authorised activity with its own rules; it is not part of this course.
  • From the outside: spraying and stuffing attempts in the logs (log analyser) show which accounts attackers think are weak.

How it is fixed

NIST SP 800-63B Revision 4 (2025) sets the modern baseline for verifiers:

  1. Length over composition: at least 15 characters for passwords used as the only factor (8 when part of multi-factor authentication), accept at least 64, allow spaces and all printable characters, and no composition rules such as "one symbol".
  2. Blocklist every new password against breached, common and context-specific values (the service name, the user name).
  3. No forced periodic changes; change on evidence of compromise.
  4. Show the estimate, not a rule list: a meter based on guess counts teaches users to pick passphrases.
  5. Slow, salted hashing (Argon2id, scrypt, bcrypt, PBKDF2) and MFA, because even good passwords get phished.

Pitfalls

  • Entropy of a random process is not the entropy of a string. tR7#qL9vX2 is strong only if it really was generated randomly; the same characters chosen as "my cat's name with substitutions" are not.
  • Overfitting the meter: users learn to satisfy it (add !1), so estimate patterns and blocklist mangled variants too.
  • Sending passwords to a third-party meter leaks them; run the estimator locally.
  • Truncation: bcrypt uses only the first 72 bytes, so a 100-character passphrase is effectively 72 bytes there.

Practice

1. Compute the charset estimate for a 12-character password of lower-case letters and digits, and the expected time at 1e10 guesses per second if it was chosen randomly.

C=36C = 36, H=12log⁡236≈62.0H = 12 \log_2 36 \approx 62.0 bits, T=2H−1/1010=3612/(2⋅1010)≈2.4×108T = 2^{H-1}/10^{10} = 36^{12}/(2 \cdot 10^{10}) \approx 2.4 \times 10^8 seconds, about 7.5 years.

2. What does the estimator say for Dragon1990$? Work it out from the model before running it.

Dictionary word dragon (13.3 bits) + capital (1) + year 1990 (7.1) + one symbol (5.0) = about 26.5 bits: reject. The naive estimate would be 11log⁡295≈72.311 \log_2 95 \approx 72.3 bits.

3. How many Diceware words do you need for at least 77 bits?

klog⁡27776≈12.9k≥77k \log_2 7776 \approx 12.9k \ge 77 gives k≥6k \ge 6 (6 words: about 77.5 bits).

Practice problem in 21 languages: Password Entropy Estimate.

Test yourself

A password is chosen uniformly at random from 62 symbols with length 10. What is its entropy?

  1. 10⋅6210 \cdot 62 bits
  2. 10log⁡262≈59.510 \log_2 62 \approx 59.5 bits
  3. 62log⁡21062 \log_2 10 bits
  4. 62 bits

Why does the estimator rate Summer2025! far lower than its charset estimate?

  1. It is too short
  2. It matches the dictionary-plus-mangling model: a common word, a capital, a year and a symbol
  3. It contains a space
  4. It is in the passphrase list

Under NIST SP 800-63B Revision 4, which is a requirement for single-factor passwords?

  1. One upper-case letter, one digit and one symbol
  2. A forced change every 90 days
  3. At least 15 characters and a blocklist check
  4. A maximum length of 16

With RR guesses per second and HH bits, the expected time to guess a random password is:

  1. 2H⋅R2^H \cdot R
  2. 2H−1/R2^{H-1}/R
  3. H/RH/R
  4. R/2HR/2^H