📖 Scenario: You are building a simple user registration system. To keep user passwords safe, you need to hash them before saving.
🎯 Goal: Create a Node.js script that uses bcrypt to hash a plain password securely.
📋 What You'll Learn
Create a variable with the plain password string exactly as specified
Set a salt rounds variable for bcrypt configuration
Use bcrypt to hash the password with the salt rounds
Export or log the hashed password variable
💡 Why This Matters
🌍 Real World
Password hashing is essential for protecting user credentials in web applications to prevent password theft.
💼 Career
Understanding bcrypt hashing is a key skill for backend developers working on authentication and security.
Progress0 / 4 steps
1
Set up the plain password variable
Create a variable called plainPassword and set it to the string "MySecret123".
Node.js
Hint
Use const to declare the variable and assign the exact string.
2
Set the salt rounds for bcrypt
Create a variable called saltRounds and set it to the number 10.
Node.js
Hint
Use const and assign the number 10 exactly.
3
Hash the password using bcrypt
Import bcrypt at the top. Then create an async function called hashPassword that uses bcrypt.hash with plainPassword and saltRounds. Inside the function, assign the hashed result to a variable called hashedPassword and return it.
Node.js
Hint
Use import bcrypt from 'bcrypt'; and an async function with await bcrypt.hash() and return hashedPassword;.
4
Export the hashed password function
Add export default hashPassword; at the end of the file to export the hashPassword function.
Node.js
Hint
Use export default followed by the function name.
Practice
(1/5)
1. What is the main purpose of using bcrypt in a Node.js application?
easy
A. To securely hash user passwords before storing them
B. To encrypt user data for faster retrieval
C. To compress files on the server
D. To generate random user IDs
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 A
Quick Check:
Password hashing = Secure storage [OK]
Hint: Bcrypt is for password security, not encryption or compression [OK]
Common Mistakes:
Confusing hashing with encryption
Thinking bcrypt compresses data
Assuming bcrypt generates IDs
2. Which of the following is the correct way to hash a password asynchronously using bcrypt in Node.js?
easy
A. const hashed = await bcrypt.hash(password, 10);
B. const hashed = bcrypt.hashSync(password, 10);
C. const hashed = bcrypt.compare(password, 10);
D. const hashed = bcrypt.hash(password);
Solution
Step 1: Identify asynchronous hashing syntax
Bcrypt's async hash function requires await and 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 A
Quick Check:
Async hash needs await and salt rounds [OK]
Hint: Async bcrypt hash needs await and salt rounds [OK]
Common Mistakes:
Using hashSync instead of async hash
Calling compare instead of hash
Omitting salt rounds argument
3. What will the following code output if the password matches the hash?
D. Missing await before bcrypt.hash causing hash to be a Promise
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 D
Quick Check:
Async hash needs await [OK]
Hint: Always await async bcrypt.hash calls [OK]
Common Mistakes:
Forgetting await on async functions
Confusing compare with hash
Using invalid salt rounds
5. You want to securely store user passwords and verify them later. Which approach correctly combines hashing and verification using bcrypt in an async function?
hard
A. Hash password synchronously with bcrypt.hashSync(password, 12) and verify asynchronously with await bcrypt.compare(input, storedHash)
B. Store the plain password and verify by hashing input with bcrypt.hashSync(input, 12) and comparing strings
C. Hash the password with await bcrypt.hash(password, 12) and verify with await bcrypt.compare(input, storedHash)
D. Encrypt password with bcrypt.encrypt(password) and decrypt for verification
Solution
Step 1: Choose secure async hashing and verification
Using async bcrypt.hash with salt rounds (12) and async bcrypt.compare is 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 C
Quick Check:
Async hash and compare with salt rounds = secure [OK]
Hint: Use async hash and compare with salt rounds for security [OK]
Common Mistakes:
Storing plain passwords
Mixing sync and async bcrypt calls
Assuming bcrypt can encrypt/decrypt
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.