Build a Password Strength Checker in Python (Full Code Included)


The best way to learn security is to build defense. Today we’ll write a password strength checker in Python — a script that grades passwords, flags weak ones, and estimates how long they’d survive a brute-force attack.

The Code

import re, math
COMMON = {"password", "123456", "qwerty", "letmein", "admin",
"welcome", "monkey", "dragon", "123456789", "password1"}
def check_password(pw: str):
score, notes = 0, []
# Length: the single biggest factor
if len(pw) >= 16: score += 3
elif len(pw) >= 12: score += 2
elif len(pw) >= 8: score += 1
else: notes.append("Too short — aim for 16+ characters.")
# Character variety
if re.search(r"[a-z]", pw): score += 1
else: notes.append("Add lowercase letters.")
if re.search(r"[A-Z]", pw): score += 1
else: notes.append("Add uppercase letters.")
if re.search(r"\d", pw): score += 1
else: notes.append("Add digits.")
if re.search(r"[^a-zA-Z0-9]", pw): score += 1
else: notes.append("Add symbols like !@#$.")
# Common password check
if pw.lower() in COMMON:
notes.append("This password is on every attacker's list!")
score = min(score, 1)
# Repeated characters
if re.search(r"(.)\1{2,}", pw):
notes.append("Avoid repeated characters (aaa, 111).")
score -= 1
# Rough crack-time estimate (10 billion guesses/sec)
charset = 26 + 26 + 10 + 32 # rough pool if all classes used

How It Works

  1. Length scoring — rewards 12+ and 16+ character passwords heavily, because length beats complexity.
  2. Character variety — checks for lowercase, uppercase, digits and symbols, nudging you to mix them.
  3. Blocklist — instantly fails passwords found in every attacker’s dictionary.
  4. Crack-time math — estimates entropy (password randomness in bits) and divides by a realistic attacker speed of 10 billion guesses per second.

Try It

Save as pwcheck.py, run python3 pwcheck.py, and test a few passwords. Watch how adding four characters changes the crack time more than adding symbols ever could.

Level Up

  • Use the zxcvbn library for professional-grade scoring.
  • Add a breach check via the HaveIBeenPwned API (it uses k-anonymity, so your password never leaves your machine).
  • Turn it into a web app with Flask.

You just built a defensive security tool. That’s how security careers start.

Comments

Popular Posts