Lesson 25 • Advanced
Password Security 🔑
Hash passwords with Argon2/bcrypt, implement strength validation, auto-rehash old hashes, and store credentials safely.
What You'll Learn in This Lesson
- • Why MD5/SHA1 are dangerous for passwords
- • Use password_hash() and password_verify() properly
- • Configure Argon2id for maximum security
- • Auto-rehash with password_needs_rehash()
- • Build a password strength checker
Password Hashing Fundamentals
PHP's password_hash() and password_verify() are the only functions you should use for password storage. They handle salting, algorithm selection, and hash formatting automatically. Never use MD5, SHA1, or any fast hash for passwords.
Try It: Password Hashing
Hash passwords with bcrypt/Argon2 and verify them securely
// Password Hashing: Why and How
console.log("=== Why Hashing Matters ===");
console.log();
console.log("If your database is breached, stored passwords are exposed.");
console.log("Hashing converts passwords into irreversible strings.");
console.log();
console.log("❌ NEVER DO THIS:");
console.log(" MD5: " + "5f4dcc3b5aa765d61d8327deb882cf99" + " (cracked in milliseconds)");
console.log(" SHA1: " + "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8" + " (rainbow tables exist)");
console.log(" P
...Argon2 & Password Policies
Argon2id is the gold standard — it resists both CPU and GPU attacks by requiring significant memory. Combined with password_needs_rehash(), you can transparently upgrade old bcrypt hashes to Argon2 when users log in. Always pair strong hashing with password strength validation.
Try It: Argon2 & Strength Checker
Configure Argon2id, auto-rehash old hashes, and validate password strength
// Argon2 & Advanced Password Policies
console.log("=== Argon2 vs Bcrypt ===");
console.log();
console.log("Bcrypt (PASSWORD_DEFAULT):");
console.log(" • CPU-intensive only");
console.log(" • 72-byte password limit");
console.log(" • Well-tested, widely supported");
console.log();
console.log("Argon2id (PASSWORD_ARGON2ID) — RECOMMENDED:");
console.log(" • CPU + Memory intensive (resists GPU attacks)");
console.log(" • No password length limit");
console.log(" • Winner of the Password Hashi
...⚠️ Common Mistakes
📋 Quick Reference — Password Security
| Function | Purpose |
|---|---|
| password_hash($pw, PASSWORD_ARGON2ID) | Hash a password (with auto-salt) |
| password_verify($input, $hash) | Check if input matches stored hash |
| password_needs_rehash($hash, algo) | Check if hash uses outdated algorithm |
| PASSWORD_DEFAULT | Bcrypt (safe, widely supported) |
| PASSWORD_ARGON2ID | Argon2id (best, memory-hard) |
🎉 Lesson Complete!
Your passwords are now bulletproof! Next, explore advanced session management with Redis and stateless tokens.
Sign up for free to track which lessons you've completed and get learning reminders.