bcryptjs Examples You Can Edit and Run
5 small bcryptjs examples written by AgentGid from the official docs, one feature each. Every one runs in your browser: change the code and see the result at once, or describe a change and let AI rewrite it. The screenshots are taken from the same code by our server, which also checks that each example starts without errors.
bcryptjs: usage and stats 193 AI-built projects use it
Hashing and checking a password with bcryptjsChange the password or the guess and run again; the salt and hash are different on every run.
Synchronous hashing in bcryptjsRun it twice: the two hashes differ every time, but both compareSync checks print true.
Salt rounds and cost in bcryptjsWatch the milliseconds grow with each round count; add 13 to the list to see it climb further.
Checking the 72-byte limit with bcryptjsEdit the samples or make the long password shorter than 72 bytes and see truncates flip to false.
Hash progress and callbacks in bcryptjsRaise the round count to 13 and you will see more progress lines before the hash prints.
Try them all in one place
import bcrypt from 'bcryptjs'
const password = 'correct horse battery staple'
// 1. Generate a salt (10 rounds) and hash the password
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(password, salt)
console.log('Password: ', password)
console.log('Salt: ', salt)
console.log('Hash: ', hash)
console.log('Hash length:', hash.length)
// 2. Later, check login attempts against the stored hash
const attempts = ['correct horse battery staple', 'Tr0ub4dor&3', 'Correct horse battery staple']
for (const guess of attempts) {
const ok = await bcrypt.compare(guess, hash)
console.log(`compare("${guess}") ->`, ok)
}
Change the password or the guess and run again; the salt and hash are different on every run. Code written by AgentGid [1]. How it works, with a screenshot →
Change the password or the guess and run again; the salt and hash are different on every run. Code written by AgentGid [1]. How it works, with a screenshot →Run it twice: the two hashes differ every time, but both compareSync checks print true. Code written by AgentGid [1]. How it works, with a screenshot →Watch the milliseconds grow with each round count; add 13 to the list to see it climb further. Code written by AgentGid [1]. How it works, with a screenshot →Edit the samples or make the long password shorter than 72 bytes and see truncates flip to false. Code written by AgentGid [1]. How it works, with a screenshot →Raise the round count to 13 and you will see more progress lines before the hash prints. Code written by AgentGid [1]. How it works, with a screenshot →Checked Oct 12, 2026: each example starts without errors in Chromium with bcryptjs 3.0.3. Needs WebGL.
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.