Passwords and Two-Factor Authentication
Your password is the front door key to your accounts. A strong password and two-factor authentication stop most account takeovers.
What Makes a Password Weak?
Attackers do not guess one password at a time by hand. They use programs that try millions of common passwords and patterns per second. These passwords are broken almost instantly:
123456,password,qwerty- Your name or city:
ali1998,karachi123 - Words with simple swaps:
P@ssw0rd - Any password that was already leaked in a data breach
What Makes a Password Strong?
| Rule | Why |
|---|---|
| Long: at least 12-16 characters | Every extra character multiplies the guesses needed |
| Unique for every account | One leak cannot open your other accounts |
| Not personal information | Birthdays and names are easy to find on social media |
| Random or a passphrase | Patterns are easy for programs to guess |
A good method is a passphrase: four or more random words, such as chai-rickshaw-mango-cloud. It is long, easy to remember and hard to guess.
Measuring Password Length
This Python program shows how many guesses an attacker needs for different password types. It uses 95 printable keyboard characters:
Example
charset = 95
for length in [8, 12, 16]:
guesses = charset ** length
print(f"{length} chars: about {guesses:.1e} guesses")8 chars: about 6.6e+15 guesses 12 chars: about 5.4e+23 guesses 16 chars: about 4.4e+31 guesses
Going from 8 to 16 characters makes the password about 6.6 million billion times harder to guess.
Generating a Random Password
Python's secrets module is made for security. It creates random passwords safely:
Example
import secrets
import string
alphabet = string.ascii_letters + string.digits + "!@#$%&*"
password = "".join(secrets.choice(alphabet) for _ in range(16))
print(password)q7R!mZp2#Lx9Wd&K
You will get a different result every time you run it.
Use the secrets module, not random, for passwords and security codes. random is predictable and is meant for games and simulations.
Password Managers
Nobody can remember 50 unique random passwords. A password manager remembers them for you:
- You remember one strong master password
- The manager creates and saves a unique password for every website
- It fills in passwords only on the real website, which also helps against phishing
Popular options include Bitwarden, 1Password and the managers built into Chrome, Edge, Firefox and Apple devices.
How Websites Store Passwords
A well-built website never stores your actual password. It stores a hash, a one-way scrambled version, with a random salt. Developers use special slow hashing functions such as bcrypt or Argon2:
Example
import bcrypt
hashed = bcrypt.hashpw(b"chai-rickshaw-mango-cloud", bcrypt.gensalt())
print(bcrypt.checkpw(b"chai-rickshaw-mango-cloud", hashed))
print(bcrypt.checkpw(b"wrong-password", hashed))True False
If the database leaks, attackers get only hashes, which are very slow to crack when the passwords are long.
Two-Factor Authentication (2FA)
2FA asks for a second proof after your password. There are three kinds of factors:
- Something you know: a password or PIN
- Something you have: your phone, an authenticator app or a security key
- Something you are: a fingerprint or face
Common 2FA methods, from weakest to strongest:
| Method | Notes |
|---|---|
| SMS code | Better than nothing, but SIM swap fraud can steal it |
| Authenticator app (Google Authenticator, Microsoft Authenticator) | Codes are made on your phone, not sent over SMS |
| Passkeys and security keys | Strongest; they also resist phishing |
Turn on 2FA for email, WhatsApp (two-step verification PIN), Facebook, Instagram, banking and mobile wallet apps first.
Never share a verification code with anyone, even if they say they are from your bank or WhatsApp support. Real companies never ask for it.
Exercise
Exercise
Which password is the strongest?
