RMRM Full Stack & AI Engineer · All guides · Roadmaps
Security · guide

Password Hashing Explained

Password hashing is a one-way cryptographic technique used to securely store user passwords so that even if a database is compromised, the original passwords cannot be easily recovered.

What Is Password Hashing?

Hashing is the process of transforming an input (like a password) into a fixed-length string of characters using a mathematical algorithm. Unlike encryption, hashing is a one-way operation — you cannot mathematically reverse a hash back to the original input. When a user logs in, their entered password is hashed and compared against the stored hash, never comparing plain text directly. Common hashing algorithms designed for passwords include bcrypt, Argon2, and scrypt.

Why It Matters for Security

Storing passwords in plain text means a single database breach exposes every user's credentials immediately. Hashing ensures that even if attackers steal the database, they only obtain hash values which are computationally expensive to reverse. This buys users and developers critical time to respond to a breach and force password resets. It is a fundamental requirement in modern security standards such as OWASP and compliance frameworks like PCI-DSS.

How Hashing Works Step by Step

When a user registers, their password is passed through a hashing function which produces a hash digest that is stored in the database. A random value called a salt is generated and mixed into the password before hashing, making each hash unique even for identical passwords. On login, the stored salt is retrieved, combined with the entered password, hashed again, and the result is compared to the stored hash. If the hashes match, authentication succeeds without ever storing or transmitting the plain-text password.

Salting and Why It Prevents Rainbow Table Attacks

A rainbow table is a precomputed lookup table mapping common passwords to their hash values, allowing attackers to instantly reverse unsalted hashes. Salting defeats this by ensuring the same password produces a completely different hash each time it is stored. Salts do not need to be secret — they are typically stored alongside the hash in the database. The security comes from the uniqueness they introduce, not from secrecy.

Choosing the Right Algorithm

General-purpose hash functions like MD5 and SHA-1 are fast by design, which makes them dangerous for password storage because attackers can test billions of guesses per second. Purpose-built password hashing algorithms such as bcrypt, scrypt, and Argon2 are deliberately slow and memory-intensive to resist brute-force and GPU-accelerated attacks. Argon2 is the current best-practice recommendation, winning the Password Hashing Competition in 2015. Always use a well-vetted library rather than implementing these algorithms yourself.

Key Gotcha: Work Factors and Future-Proofing

Password hashing algorithms include a configurable work factor (also called cost or iterations) that controls how long hashing takes. As hardware gets faster over time, you should periodically increase the work factor to keep hashing expensive for attackers. A good rule of thumb is to target a hashing time of 100–300 milliseconds on your production hardware. Re-hash stored passwords with updated work factors transparently at the next user login to avoid forced resets.

Go deeper with an AI tutor that teaches this in context — and quizzes you on it.
Open the app — free to start

© RM Full Stack & AI Engineer · All guides · Roadmaps · Open the app