8. Authentication & Password Security
What is Authentication?
Authentication is verifying that a user is who they claim to be. The most common method is email and password login. But storing passwords in plain text is a massive security risk.
Hashing Passwords
Hashing is a one-way function that converts a password into a fixed-length string that cannot be reversed. This is why you NEVER store plain text passwords. If your database is hacked, plain text passwords give attackers access to your users' accounts everywhere. With hashing, they only get garbage.
const crypto = require('crypto'); const password = 'mySecret123'; const hash = crypto.createHash('sha256').update(password).digest('hex'); console.log('Password:', password); console.log('Hash:', hash);
Salts
A salt is random data added to the password before hashing. Without a salt, two users with the same password would have identical hashes. An attacker could see this pattern. With a salt, every hash is unique, even for identical passwords.
const crypto = require('crypto'); const password = 'password123'; const salt1 = crypto.randomBytes(16).toString('hex'); const salt2 = crypto.randomBytes(16).toString('hex'); const hash1 = crypto.pbkdf2Sync(password, salt1, 1000, 64, 'sha256').toString('hex'); const hash2 = crypto.pbkdf2Sync(password, salt2, 1000, 64, 'sha256').toString('hex'); console.log('Same password, different salts'); console.log('Equal?', hash1 === hash2);
Using bcrypt
bcrypt is a library designed specifically for password hashing. It automatically generates salts and is intentionally slow. Why slow? Because an attacker trying to guess billions of passwords will be dramatically slowed down. A modern GPU can do 1 billion SHA256 hashes per second but only 50,000 bcrypt hashes.
const bcrypt = require('bcrypt'); async function demo() { const password = 'mySecurePassword'; const hash = await bcrypt.hash(password, 10); console.log('Hash:', hash); const isValid = await bcrypt.compare(password, hash); console.log('Password match:', isValid); } demo();
Salt Rounds
The number passed to bcrypt.hash() (like 10 or 12) controls how many hashing iterations occur: 2^rounds iterations. A rounds value of 10 means 1,024 iterations. Production systems typically use 12-14. As computers get faster, you can increase the rounds to maintain security.
const bcrypt = require('bcrypt'); async function compareSpeed() { const password = 'test'; console.time('Rounds 10'); await bcrypt.hash(password, 10); console.timeEnd('Rounds 10'); console.time('Rounds 12'); await bcrypt.hash(password, 12); console.timeEnd('Rounds 12'); } compareSpeed();
Registration Flow
When a user registers, you take their plain password, hash it with bcrypt, and store only the hash in your database. The plain password is never stored anywhere.
const bcrypt = require('bcrypt'); async function registerUser(email, plainPassword) { const hashedPassword = await bcrypt.hash(plainPassword, 12); // Store email + hashedPassword in database console.log('Saving to DB:', { email, passwordHash: hashedPassword }); return { userId: 123, email }; } registerUser('alice@example.com', 'alice123');
Login Flow
When a user logs in, you look up their email in the database to get the stored hash. Then you use bcrypt.compare() to hash the password they just typed and compare it to the stored hash. If they match, the password was correct. You never decrypt anything.
const bcrypt = require('bcrypt'); const storedHash = '$2b$10$CwTycUXWue0Thq9StjUM0uJQQvK7hq6WqE9ZqWqE9ZqWqE9ZqWq'; async function loginUser(email, plainPassword) { const isValid = await bcrypt.compare(plainPassword, storedHash); if (isValid) { console.log('Login successful!'); return { success: true }; } else { console.log('Invalid password'); return { success: false }; } } loginUser('alice@example.com', 'alice123');
Knowledge Check
Ready to test your understanding of 8. Authentication & Password Security?