Passwords done right
Understand the idea
Store a salted, memory-hard verifier. Do not store a password you can decrypt.
The idea
Hash the password. Do not encrypt it. is one way: you store the verifier and recompute it at login. Encryption keeps a key that can turn the stored value back into the password, which is the bug in the second run. Each user also gets a unique so identical passwords do not share a hash. Do not roll your own hash either. SHA-256 of the password, the third run, has no salt and no memory parameter, so the same password always yields the same digest and a GPU can try it at full speed. The working example uses Node's built-in scrypt, which is memory-hard. Production systems should use Argon2id from a reviewed library. The reason Argon2 replaced bcrypt is the memory cost, not fashion.
What changes
| Old approach | Weakness that showed up | What replaced it |
|---|---|---|
| bcrypt with its default cost | Each guess uses a few kilobytes. A GPU or ASIC can run enormous batches in parallel because memory per guess stays tiny. | Argon2id allocates a large, configurable memory buffer per guess. Extra cores do not help much unless each core also gets that memory, which is expensive to scale. |
The command
Salted scrypt verify
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Encryption gives the password back
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
A raw hash has no memory cost
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
The first run prints true, then false. The second run prints correct horse, because AES-GCM with the server key is reversible. That second result is the failure mode.
Guided challenge
Reject a wrong password
verifyPassword currently returns true for every password. Restore the timing-safe compare so a wrong password prints false.
Reject a wrong password
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
true false
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.