Node.js - why bcrypt generate different outputs for same input

3 points

I'm using bcrypt module to hash users' passwords in my database,  but I'm confused because for the same input I get different outputs.

Could someone explain, why bcrypt behaves like this?

1 answer
0 points
Answered by:
Dirask Admin - Marvin

Without going into details of the implementation of the hashing algorithm, bcrypt returns a different value on the output each time, which depends on the random value of the 'salt' (saltRounds).
This makes it much more difficult to crack the encrypted password.

Check out the example below

const bcrypt = require('bcrypt');

const saltRounds = 10;
const password = '123456';

for (let i = 0; i < 5; i++) {
    let hashedPassword;

    bcrypt.hash(password, saltRounds, (error, hash) => { // encoding password 
        if (error) console.log(error);
        hashedPassword = hash;, hashedPassword, (error, result) => { 
            if (error) console.log(error);
            console.log(result);  // result is true when password and hashedPassword match



After output, we can see that the encrypted password is different each time, but the result of each comparison is true.

You can also check this article:
Node.js - hash passwords using bcrypt

0 comments Add comment
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂