Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    Storing Passwords

    Rate-limit logins (Ch 11) — slow hashing protects a stolen table; it doesn't stop online guessing against your login page.

    Chapter 3 of 135 min readOften asked in interviews

    The Plain Picture#

    A club doesn't keep a list of members' secret handshakes. Instead, for each member it keeps a sealed impression of the handshake that can be checked but not copied. And to stop a thief from preparing fake impressions in advance, every member's impression is made with their own unique extra twist. Finally, the impression machine is deliberately slow — one check takes a moment, so checking millions of guesses takes forever.

    The Real Names#

    1. Never store passwords in plain text. A single leak exposes every account — and every other site where those users reused the password (Part 0, problem 1).

    2. A plain hash isn't enough. Attackers have pre-computed huge tables mapping hashes back to common passwords — rainbow tables — and they can compute billions of fast hashes per second on graphics cards.

    3. A salt is a random value, unique to each user, mixed into the password before hashing and stored next to the hash. Two users with the same password get different hashes, and pre-computed tables become useless.

    4. A slow, memory-hard password-hashing function makes each guess expensive. The accepted choices: Argon2id (the modern first choice), scrypt, and bcrypt (older, still fine and very common).

    Under the Hood#

    Why fast hashes fail — real timings on this Mac#

    Output
      SHA-256, 100,000 hashes of "password123"   →  31 ms total    (≈ 0.3 µs per guess)
      scrypt (N=2^14, r=8, p=1), 1 hash          →  36 ms          (≈ 100,000× slower per guess)

    The plain SHA-256 hash of password123 is always:

    Output
    ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f

    Search for that string online and you'll find it listed as "password123" — that's a rainbow table in action. Every user with that password has that identical hash.

    An attacker with a leaked table of plain SHA-256 hashes can try millions of guesses per second on a laptop (billions with GPUs). With scrypt at 36 ms each, the same laptop manages about 28 guesses per second. That difference is the whole point.

    What is actually stored#

      users table
      ┌───────────┬───────────────────────────────────────────────────────────────────────┐
      │ email     │ password_hash                                                         │
      ├───────────┼───────────────────────────────────────────────────────────────────────┤
      │ priya@... │ $argon2id$v=19$m=65536,t=3,p=4$<salt, base64>$<hash, base64>          │
      └───────────┴───────────────────────────────────────────────────────────────────────┘
                   algorithm   version  cost settings   salt            the result

    The algorithm name, its cost settings and the salt are stored inside the hash string, so the settings can be increased over time (re-hash each user's password with the new settings the next time they log in).

    Login check:

    Output
      user types password → read that user's stored string → take salt + settings from it
      → hash the typed password the same way → compare with the stored hash (in constant time)
      → equal? logged in

    "Constant time" means the comparison takes the same time whether the first character or the last one differs — otherwise response timing leaks information. Libraries handle this; in Python, hmac.compare_digest.

    In code, never build this yourself. Use a library: argon2-cffi or passlib/pwdlib in Python (Authentication and Authorization (coming soon)), Spring Security's password encoders in Java.

    Gotchas

    • Rate-limit logins (Ch 11) — slow hashing protects a stolen table; it doesn't stop online guessing against your login page.

    • Never log passwords, even by accident (request-body logging of /login is a classic leak).

    • Password rules: modern guidance (NIST) says: require length (at least 8; allow long passphrases), check new passwords against lists of known-breached ones, and don't force arbitrary complexity rules or periodic changes.

    Check yourself

    4 questions
    1

    Why isn't a plain SHA-256 hash good enough for passwords?

    Show answer

    It's fast, so attackers can test billions of guesses; and identical passwords give identical hashes, which pre-computed rainbow tables reverse instantly.

    2

    What does a salt do?

    Show answer

    Makes each user's hash unique even for the same password, defeating pre-computed tables and forcing attackers to crack each user separately.

    3

    Name three acceptable password-hashing functions.

    Show answer

    Argon2id, scrypt, bcrypt.

    4

    Where is the salt stored, and is it secret?

    Show answer

    Next to (usually inside) the hash string. It isn't secret — its job is uniqueness, not secrecy.