Cybersecurity Tutorial menu

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?

RuleWhy
Long: at least 12-16 charactersEvery extra character multiplies the guesses needed
Unique for every accountOne leak cannot open your other accounts
Not personal informationBirthdays and names are easy to find on social media
Random or a passphrasePatterns 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

python
charset = 95
for length in [8, 12, 16]:
    guesses = charset ** length
    print(f"{length} chars: about {guesses:.1e} guesses")
Output
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

python
import secrets
import string

alphabet = string.ascii_letters + string.digits + "!@#$%&*"
password = "".join(secrets.choice(alphabet) for _ in range(16))
print(password)
Output
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

python
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))
Output
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:

  1. Something you know: a password or PIN
  2. Something you have: your phone, an authenticator app or a security key
  3. Something you are: a fingerprint or face

Common 2FA methods, from weakest to strongest:

MethodNotes
SMS codeBetter 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 keysStrongest; 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?

Learn Cybersecurity with a teacher

Join the CIT (Computer Information Technology) course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp