Back to cheatsheets
Passwords
bcrypt Password Hashing
Generate and verify bcrypt hashes with adjustable cost factors for password storage.
What you'll learn
- bcrypt hash (Node.js)
- Cost factor guidance
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
2 ready-to-use snippets for bcrypt password hashing. Click copy and paste into your project.
bcrypt hash (Node.js)
const bcrypt = require('bcryptjs');
// Hash with cost factor 12
async function hashPassword(pw) {
const salt = await bcrypt.genSalt(12);
return bcrypt.hash(pw, salt);
}
// Verify
async function verifyPassword(pw, hash) {
return bcrypt.compare(pw, hash);
}
Cost factor guidance
// Target: 300-500ms per hash on production hardware
// Cost 10 → ~100ms (minimum acceptable)
// Cost 12 → ~300ms (recommended 2025+)
// Cost 14 → ~1.2s (high security)
// Cost 16 → ~5s (paranoid)
//
// bcrypt limits passwords to 72 bytes.
// Pre-hash with SHA-256 if longer keys needed:
const preHash = await crypto.subtle.digest(
'SHA-256', new TextEncoder().encode(pw)
);
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site