Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between hash() and hashSync() functions of BCrypt package of NodeJs

const bcrypt = require('bcrypt')

const hash = bcrypt.hash(<myPassword>, 12)

const hashSync = bcrypt.hashSync(<myPasword>, 12)

What aspects do they possibly differ in, and Could they be used interchangeably? (Detailed explanation very much welcome and greatly appreciated!)

like image 779
Nikhil K Mannem Avatar asked Oct 16 '25 02:10

Nikhil K Mannem


2 Answers

hashSync is used to Synchronously generates a hash for the given string. It returns the hashed string

hash is used for Asynchronously generating a hash for the given string. It returns promise is callback is committed and you need to resolve the promise.

refer https://www.npmjs.com/package/bcryptjs#hashsyncs-salt

like image 71
Aditya Joshi Avatar answered Oct 17 '25 15:10

Aditya Joshi


bcrypt.hash takes a callback as its third parameter which will be called when the hash is completed. bcrypt.hashSync runs the hash, waits for it to complete and returns the hashed value.

In other words "hash" is asynchronous and hashSync is synschronous.

like image 35
Ian Avatar answered Oct 17 '25 16:10

Ian