Synchronous hashing in bcryptjs
The sync API returns results directly instead of a promise, which keeps scripts and small tools short. The same hash format works with the async API.
How it works
- Salt, then hash.
bcrypt.genSaltSync(10)returns a salt string, andbcrypt.hashSync(password, salt)returns the hash right away. [1] - Auto-generate the salt. Passing a number instead of a salt, as in
bcrypt.hashSync(password, 10), creates the salt for you. Hashing the same password twice gives two different hashes because each gets its own salt. [1] - Verify with compareSync.
bcrypt.compareSync(password, hash)returns a boolean. Both hashes validate the same password. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import bcrypt from 'bcryptjs'
const password = 'hunter2'
// Explicit salt
const salt = bcrypt.genSaltSync(10)
const hashA = bcrypt.hashSync(password, salt)
// Salt generated from a round count
const hashB = bcrypt.hashSync(password, 10)
console.log('Salt: ', salt)
console.log('Hash A: ', hashA)
console.log('Hash B: ', hashB)
console.log('Same hash twice?', hashA === hashB)
console.log('compareSync(password, hashA):', bcrypt.compareSync(password, hashA))
console.log('compareSync(password, hashB):', bcrypt.compareSync(password, hashB))
console.log('compareSync("hunter3", hashA):', bcrypt.compareSync('hunter3', hashA))
Run it twice: the two hashes differ every time, but both compareSync checks print true. Code written by AgentGid [1].
Run it twice: the two hashes differ every time, but both compareSync checks print true. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with bcryptjs 3.0.3. Needs WebGL.
More bcryptjs examples
Hashing and checking a password with bcryptjs Salt rounds and cost in bcryptjs Checking the 72-byte limit with bcryptjs Hash progress and callbacks in bcryptjs
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.