Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 31 ยท Projects

Project: Password Policy Checker

Build a password policy checker in Python: estimate entropy, reject banned and patterned passwords, store salted hashes and give helpful feedback to users.

A password rule such as "at least 8 characters, one capital, one digit" sounds strict, yet Password1! passes it and is among the first guesses any attacker tries. Good policy checks the things that actually predict guessability: length, predictable patterns, membership in lists of common passwords, and how the password will be stored once accepted. A good checker also explains itself, because a user who understands the message picks a better password.

In this project you build a password policy checker in Python. It estimates strength, rejects banned and patterned passwords, shows how to store a password as a salted, slow hash, and returns feedback in plain language. Everything runs on invented sample passwords. It never touches real accounts and never cracks anything. For the theory behind each part, see Password Storage and Hashing and Password Strength, Entropy and Cracking Resistance.

Real versus stand-in. In production you would use a vetted library for hashing (bcrypt, scrypt or Argon2, for example through your framework) and a breached-password service or a large local list, not a ten-word set. This lesson uses a small banned list and a hand-written PBKDF2 built from hmac, because the browser's Python has no hashlib.pbkdf2_hmac. The structure of the checks is real; the data sizes and the hash function are teaching stand-ins.

What the checker does

Four questions per password

How strong? Estimate guesses, not just characters.
Is it banned? Common words and tricks.
How is it stored? Salted and deliberately slow.
What should the user do? Clear, specific feedback.

Rules that block real weaknesses beat rules that count symbols.

1 / 2

Stage 1: a rough entropy estimate

Entropy measures unpredictability in bits: each bit doubles the number of guesses an attacker needs. For a password chosen uniformly at random from a character set, the formula is length x log2(pool size), where the pool is the set of characters the password could draw from. Lowercase letters give a pool of 26, adding capitals makes 52, digits make 62, and symbols raise it further.

That formula is only valid for random choices. A human-chosen Summer2026! has the same length and pool as a random string, yet an attacker guesses it in a handful of attempts. So the naive number is an upper bound: it can overrate, but it never underrates. The sample below makes that gap visible.

Python Stage 1: naive entropy and its limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Study the table. aaaaaaaaaaaa and Summer2026! score well on the naive scale, although neither would survive a real guessing attack. The last line shows the honest way to rate a passphrase: count words, not letters. Four random words from a 7,776-word list give about 51.7 bits, and that figure is only true if the words really were chosen at random, for example by dice or a generator. A phrase you invented yourself is far weaker.

Stage 2: banned lists and pattern checks

The most effective single rule is to reject passwords that attackers try first. A banned list holds the most common passwords, plus the user's own name and the site name. Attackers also know the usual tricks, so a check must see through them: capital first letter, a number at the end, @ for a, 0 for o. We therefore normalize the candidate before comparing.

We add three more cheap checks that catch lazy choices: the username inside the password, long runs of one repeated character, and keyboard or alphabet sequences such as abcd and 1234.

Python Stage 2: banned and patterned passwords

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two design notes. First, the checks collect every problem instead of stopping at the first one, so a user fixes everything in a single attempt. Second, the policy rejects Summer2026! even though it satisfies the old "capital, digit, symbol" rule. That is exactly the point: composition rules reward predictable substitutions, while a list of known-bad choices stops them. Current guidance (for example from NIST) recommends checking against breached and common passwords and dropping forced composition rules and routine expiry.

Stage 3: store passwords as salted, slow hashes

Accepting a good password is only half the job. The server must never store it in readable form. Instead it stores a hash: a one-way fingerprint. Two additions make the hash resistant to attack:

  • A salt: a random value, unique per user, mixed into the hash. Two users with the same password then get different hashes, and precomputed tables become useless.
  • A work factor: the hash function repeats its core step many thousands of times, so each guess costs the attacker real time. A fast hash like plain SHA-256 is wrong for passwords because attackers can compute billions per second.

PBKDF2 is the simplest algorithm that has both. Production systems prefer bcrypt, scrypt or Argon2, but the principles are identical. The function below is a short, spec-accurate PBKDF2-HMAC-SHA256, built from hmac because this browser runtime lacks hashlib.pbkdf2_hmac.

Python Stage 3: salted, iterated hashing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three details deserve attention. The record stores the scheme, the iteration count and the salt next to the hash, so you can raise the iteration count later and still verify old records. hmac.compare_digest compares in constant time, which avoids leaking how many leading characters matched. And salts come from os.urandom, a cryptographic source, never from random.

The loop is also your cost dial: doubling ITERATIONS doubles the work for both you (at login) and an attacker (per guess). That is why the number must be tuned: high enough to hurt attackers, low enough that a login still takes a fraction of a second.

Stage 4: the complete checker with feedback

The final stage combines everything. It estimates an effective strength: the naive number, reduced when we know the password is predictable. A banned password is capped near zero, a password containing the username is capped low, and patterns halve the estimate. Words separated by dashes or spaces are rated by word count using a conservative 2,048-word assumption (11 bits per word), because the checker cannot know whether the user chose them randomly. The result is a label plus tailored advice.

Python Stage 4: score and advice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The bit values are estimates for teaching, not security guarantees; real estimators such as zxcvbn model many more patterns (dates, keyboard walks, names, dictionary words). What matters is the shape of the code: independent checks, each adding a message, one score that is only ever lowered by evidence of predictability, and advice the user can act on. Also notice that the checker never logs or stores the plaintext password; it only reads it, rates it and discards it.

Ideas to extend the project

  • Load a larger banned list from a file and use a set so lookups stay instant.
  • Support the "have I been pwned"-style k-anonymity check: send only the first five characters of the password's SHA-1 hash to the service and compare the rest locally.
  • Add rate limiting and lockout rules for the login endpoint, see Auditing Your Own Credential Policy.
  • Support upgrading hashes at login: when a stored record has fewer iterations than today's target, re-hash it with the new settings after a successful login.
  • Add unit tests with a table of passwords and the label each must receive.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is Summer2026! rejected by this checker although it has a capital, a digit and a symbol?

    Composition rules reward predictable substitutions. The checker normalizes the candidate and finds the common word "summer" under the decoration.
  2. 2. What does a per-user salt prevent?

    A random salt makes each stored hash unique, so one table of precomputed hashes cannot be applied to every account.
  3. 3. Why is plain SHA-256 a poor choice for storing passwords?

    Password hashing should be deliberately expensive. Fast general-purpose hashes help attackers, while bcrypt, scrypt, Argon2 and iterated PBKDF2 slow each guess down.
  4. 4. Why does the checker rate a dashed word passphrase by word count instead of by letters?

    An attacker guessing a passphrase picks words from a list, so the number of words and the size of that list set the guesses needed, not the character count.

Summary

  • Entropy is length x log2(pool) only for random choices; for human-made passwords the naive figure is an upper bound.
  • Reject known-bad passwords and their predictable variations; this beats counting capitals and symbols.
  • Report all problems at once, in plain words, with a concrete next step.
  • Store only a salted, slow hash of the password: PBKDF2, bcrypt, scrypt or Argon2, with the parameters kept in the record.
  • Compare hashes in constant time and take salts from a cryptographic random source.
  • Tune the work factor so logins stay fast while each guess stays expensive.

Next project: Project: Vulnerability Risk Register.