What if your users' passwords were stolen tomorrow? Bcrypt helps stop that nightmare.
Why Password hashing with bcrypt in Node.js? - Purpose & Use Cases
Imagine you store user passwords as plain text in your database. If someone hacks your system, they get all passwords instantly.
Storing plain passwords is risky and careless. Manually creating your own encryption is complicated and often insecure. It's easy to make mistakes that expose user data.
Using bcrypt automatically hashes passwords with strong, tested algorithms and adds a unique salt. This makes stolen data useless to attackers.
const password = 'userpass123'; // Store password directly in DB saveToDB(password);
const bcrypt = require('bcrypt'); (async () => { const hashed = await bcrypt.hash('userpass123', 10); saveToDB(hashed); })();
It enables secure password storage that protects users even if your database is compromised.
When you sign up on a website, your password is hashed with bcrypt before saving, so even if hackers get the data, they can't see your real password.
Storing plain passwords is dangerous and easy to exploit.
Bcrypt hashes passwords with salt to keep them safe.
Using bcrypt protects users and builds trust in your app.
Practice
bcrypt in a Node.js application?Solution
Step 1: Understand bcrypt's role
Bcrypt is designed to hash passwords securely, making them hard to reverse.Step 2: Identify the correct purpose
Encrypting data or compressing files are not bcrypt's functions; it focuses on password hashing.Final Answer:
To securely hash user passwords before storing them -> Option AQuick Check:
Password hashing = Secure storage [OK]
- Confusing hashing with encryption
- Thinking bcrypt compresses data
- Assuming bcrypt generates IDs
Solution
Step 1: Identify asynchronous hashing syntax
Bcrypt's async hash function requiresawaitand two arguments: password and salt rounds.Step 2: Check each option
hashSync uses the synchronous method, compare is for verification not hashing, and the hash call misses salt rounds.Final Answer:
const hashed = await bcrypt.hash(password, 10); -> Option AQuick Check:
Async hash needs await and salt rounds [OK]
- Using hashSync instead of async hash
- Calling compare instead of hash
- Omitting salt rounds argument
const bcrypt = require('bcrypt');
async function check() {
const password = 'secret123';
const hash = await bcrypt.hash(password, 10);
const result = await bcrypt.compare('secret123', hash);
console.log(result);
}
check();Solution
Step 1: Understand bcrypt.compare behavior
It returns true if the plain password matches the hashed password.Step 2: Analyze the code flow
The password 'secret123' is hashed and then compared with the same password, so result is true.Final Answer:
true -> Option BQuick Check:
Matching password returns true [OK]
- Expecting false for matching passwords
- Confusing compare with hash
- Ignoring async/await in code
const bcrypt = require('bcrypt');
async function verify() {
const password = 'mypassword';
const hash = bcrypt.hash(password, 10);
const match = await bcrypt.compare(password, hash);
console.log(match);
}
verify();Solution
Step 1: Check bcrypt.hash usage
Bcrypt.hash is async and returns a Promise, so it needs await to get the hashed string.Step 2: Identify the error impact
Without await, hash is a Promise, causing compare to fail or behave unexpectedly.Final Answer:
Missing await before bcrypt.hash causing hash to be a Promise -> Option DQuick Check:
Async hash needs await [OK]
- Forgetting await on async functions
- Confusing compare with hash
- Using invalid salt rounds
Solution
Step 1: Choose secure async hashing and verification
Using asyncbcrypt.hashwith salt rounds (12) and asyncbcrypt.compareis best practice.Step 2: Evaluate other options
Storing plain passwords and hashing input for string comparison is insecure, mixing sync hashing with async verification is inconsistent, and bcrypt has no encrypt/decrypt methods.Final Answer:
Hash the password with await bcrypt.hash(password, 12) and verify with await bcrypt.compare(input, storedHash) -> Option CQuick Check:
Async hash and compare with salt rounds = secure [OK]
- Storing plain passwords
- Mixing sync and async bcrypt calls
- Assuming bcrypt can encrypt/decrypt
Ready to prove it?
Pass the test and this pattern moves unknown
