Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Am new to javascript and nodejs, I wrote a code to do authentication of the user from the password and salt present in database, Below is the code in nodejs were i receive a usercode and password and i retrieve the data from database and compare the password and salt present in DB with the received password.

The salt stored in DB is generated by base64 format.

var Bcrypt = require('bcrypt');   
var pg = require('pg');
var usercode = 'tarun';
var clientid='214057357158656';
var password='tarun';
var connectionString = "postgres://dbusername:password@localhost:5432/USCProduction";
console.log('connectin to DB');
var client = new pg.Client(connectionString);
client.connect(function(err) {   
    if(err) {
        console.log(err);
    var Query ='select password, salt from muser, mclient where usercode='+"'"+usercode+"'"+' and muser.clientid='+clientid+' and muser.clientid=mclient.clientid and mclient.status=1';
    console.log('executing query',Query);
    client.query(Query, function(err, result) {
   if(err){
       console.log('Error in executing Query');
       client.end();
   } else {
       console.log(result.rows);
       var passinDB=result.rows[0].password;
       var saltinDB=result.rows[0].salt;
       console.log('passwordinDB : ',passinDB);
       console.log('saltinDB : ',saltinDB);
 client.end();
    Bcrypt.hash(passinDB, saltinDB, function(err, hash) {
        if(err) {
                return console.error(err);
        console.log(hash);
    Bcrypt.compare(password, hash, function(err, isMatch) {
        if(err) {
                return console.error(err);
        console.log('do they match?', isMatch);

Am facing the following error while ececuting the code

passwordinDB :  StAxL1r3bb/5k/6D6+BulwxhXFs=
saltinDB :  FOhs8crXyO8=
    [Error: Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue]

its unable to find number of rounds of the salt or any other i may be missing in the code ,how to over come this error.

Thank You..!!

$Vers is bcrypt_id
$log2(NumRounds) is a parameter that tells it how many times to execute that internal hash function.

So you need to generate salt and has password in this way:

var bcrypt = require('bcryptjs');
bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash("any_password", salt, function(err, hash) {
        // Store hash in your password DB.
                .then(hash => {
                    console.log(`INFO: Hash generated: ${hash}`)
                        // Store hash in your password DB.
    ).catch(err => console.log(err));

Console

jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Intermediate-Node-API$ npm run dev
> intermediate-node-api@1.0.0 dev /home/jmendoza/IdeaProjects/NodeJS-API-Course/Intermediate-Node-API
> nodemon src/app.js
[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/app.js`
INFO: A NodeJS API is listining on port: 3000
INFO: mongoDB, Atlas. Connected
INFO: SALT: 15
POST /api/v1/users 200 28.146 ms - 40
INFO: Hash generated: $2b$10$1/4o27jmhscGEgTyF54NQurLbZvXI9zFROZKhlm0EoHsBE24NTAVu

You can see the complete code in my repository:

https://github.com/JonathanM2ndoza/NodeJS-API-Course/blob/master/Advanced-Node-API/src/modules/encrypt.ts

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.