Password hashing runs a password through a one-way function that produces a fixed fingerprint which cannot be turned back into the password. You store the hash, not the password, so if your database leaks, the actual passwords are not exposed. To check a login you hash the attempt and compare, never decrypting anything.
Why hashing, not encryption
Encryption is reversible: anyone with the key can recover the password, so a key leak is a password leak. Hashing is one-way by design, so there is nothing to reverse. You never need the original password back, only to check whether a submitted one matches, which a hash does perfectly.
Why the algorithm matters
Fast hashes like MD5 or SHA-256 are wrong for passwords, because an attacker can compute billions of guesses per second against a leaked database. Use a slow, salted, purpose-built algorithm such as bcrypt, scrypt, or argon2, which are deliberately expensive to compute and include a per-password salt to defeat precomputed tables.
What this means for AI-generated code
AI tools sometimes reach for a plain SHA-256 or even store passwords with light or no hashing, because it produces working login code. The difference only matters after a breach, so the weak choice passes every test until it is too late.
Common questions
What is the correct way to hash passwords in Node.js?
Use bcrypt or argon2. With bcrypt: await bcrypt.hash(password, 12) to store, and await bcrypt.compare(input, stored) to verify. The second argument to hash() is the cost factor — 12 is the current minimum recommendation. Never use crypto.createHash('sha256') or similar for passwords; those are fast hashes designed for data integrity, not password storage.
Why is SHA-256 wrong for passwords?
SHA-256 is fast — modern hardware can compute billions of SHA-256 hashes per second. That means an attacker with a leaked database can try billions of password guesses per second. bcrypt and argon2 are intentionally slow and have a tunable cost factor so you can increase the cost as hardware gets faster, keeping brute-force infeasible.
What is a password salt?
A salt is a random value added to the password before hashing, unique for each stored password. Without a salt, two users with the same password produce the same hash, and precomputed rainbow tables can crack many hashes at once. bcrypt and argon2 include the salt in their output automatically — you do not manage it separately.