- Which hashing algorithm should I use for password storage?
- For password storage in production systems, use adaptive algorithms like bcrypt, Argon2, or scrypt - never MD5 or SHA-1. These adaptive algorithms are specifically designed for passwords with built-in salt support and configurable work factors that slow down brute-force attacks. Argon2 is recommended by OWASP as the first choice (winner of Password Hashing Competition 2015), followed by scrypt and bcrypt. Avoid fast algorithms (MD5, SHA-256) for passwords as they enable rapid brute-force attacks with modern GPUs.
- What is a salt and why is it necessary for password hashing?
- A salt is random data (typically 16+ bytes) added to each password before hashing. Without salt, identical passwords produce identical hashes, enabling rainbow table attacks (precomputed hash databases). Salt ensures each password gets a unique hash even if passwords match. The salt should be: cryptographically random (using secure RNG), unique per password (never reused), stored alongside the hash (not secret), and sufficiently long (128+ bits). Modern algorithms like bcrypt automatically handle salting.
- How do I verify a password against a stored hash?
- To verify a password: 1) Retrieve the stored hash and salt from your database, 2) Hash the user-provided password using the same algorithm, salt, and parameters, 3) Compare the newly generated hash with the stored hash using constant-time comparison (to prevent timing attacks). Never decrypt or reverse the hash - hashing is one-way by design. For bcrypt/scrypt/Argon2, use their built-in verify functions which handle salt extraction and comparison securely.
- Is it safe to hash passwords in the browser?
- Hashing passwords in the browser (client-side) alone is NOT secure for authentication. If the server only stores client-hashed passwords, attackers who steal the database can authenticate directly with stolen hashes (pass-the-hash attack). Best practice: hash on the server using bcrypt/Argon2 after receiving the password over HTTPS. Optional client-side hashing can add defense-in-depth (preventing server from seeing plaintext), but server-side hashing with proper salt and work factor remains essential. Always use HTTPS to protect passwords in transit.