How to use the hash function from bcrypt-nodejs

Find comprehensive JavaScript bcrypt-nodejs.hash code examples handpicked from public code repositorys.

bcrypt-nodejs.hash is a function that generates a bcrypt hash from a plain text password.

0
1
2
3
4
5
6
7
8
9
const bluebird = require('bluebird');
const bcrypt = bluebird.promisifyAll(require('bcrypt-nodejs'));

function storePassword(password, callback, async=false) {
    if(!async) {
        bcrypt.hash(password, null, null, function(err, hash) {
            callback(err, hash);
        });
    }
    else {
fork icon4
star icon4
watch icon2

13
14
15
16
17
18
19
20
21
22
data.email = data.email.toLowerCase();
if (data.email.includes("@") && data.email.includes(".")) {
    clientes_arr = await Cliente.find({ email: data.email });
    if (clientes_arr.length == 0) {
        if (data.password) {
            bcrypt.hash(data.password, null, null, async function (err, hash) {
                if (hash) {
                    data.password = hash;
                    data.tipo = data.tipo.toLowerCase();
                    var reg = await Cliente.create(data);
fork icon0
star icon0
watch icon1

+ 8 other calls in file

How does bcrypt-nodejs.hash work?

bcrypt-nodejs.hash() is a function that creates a hash string from a given plaintext password using the bcrypt algorithm, which is a widely used method for secure password storage. The function takes the password string and a salt round value as input, and returns a Promise that resolves to the resulting hash string. The higher the salt round value, the longer the time it takes to generate the hash and the more secure it is.

12
13
14
15
16
17
18
19
20
21
//registro
usuarios_arr = await Usuario.find({email:data.email});

if(usuarios_arr.length == 0){
    if(data.password){
        bcrypt.hash(data.password,null,null, async function(err, hash){
            if(hash){
                data.password = hash;
                var reg = await Usuario.create(data);
                //console.log(hash);
fork icon0
star icon0
watch icon1

+ 9 other calls in file

10
11
12
13
14
15
16
17
18
19

admin_arr = await Admin.find({email:data.email});

if(admin_arr.length == 0){
    if(data.password){
        bcrypt.hash(data.password, null, null, async function(err, hash){
             if(hash){
                 data.password = hash;
                var reg = await Admin.create(data);
                res.status(200).send({data: reg});
fork icon0
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const bcrypt = require("bcrypt-nodejs");

const password = "myPassword123";
const saltRounds = 10;

bcrypt.hash(password, null, null, function (err, hash) {
  if (err) {
    throw err;
  }

  // Store the hash in a database or other persistent storage
  console.log(hash);
});

In this example, we first define the password we want to hash, and then specify the saltRounds to be used for generating the salt. We then call the bcrypt.hash function with the password, null for the salt, and null for the progress callback. The result is a hash of the password that can be stored in a database or other persistent storage for future comparison.

104
105
106
107
108
109
110
111
112
113
114
const registro_cliente_admin = async function(req,res){
    if(req.user){
        if(req.user.role == 'admin'){
            var data = req.body;


            bcrypt.hash('123456789',null,null,async function(err,hash){
                if(hash){
                    data.password = hash;
                    let reg = await Cliente.create(data);
                    res.status(200).send({data:reg});
fork icon0
star icon0
watch icon1

+ 11 other calls in file

8
9
10
11
12
13
14
15
16
17
let usuarios = await Usuario.find({email: data.email});

if(usuarios.length >= 1){
    res.status(200).send({data:undefined,message: 'El correo electrónico ya existe'});
}else{
    bcrypt.hash('123456',null,null, async function(err,hash){
        if(err){
            res.status(200).send({data:undefined,message: 'No se pudo encriptar la contraseña'});
        }else{
            data.password = hash;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

8
9
10
11
12
13
14
15
16
17

if(reg.length >= 1){
    res.status(200).send({message: 'El correo electrónico ya existe'});
}else{

    bcrypt.hash(data.password,null,null,async function(err,hash){
        if(err){
            res.status(200).send({message: 'Problema durante la encriptación'});
        }else{
            data.password = hash;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

32
33
34
35
36
37
38
39
40
41

if(oneUser && oneUser.length >=1) {
    throw boom.conflict("USER ALREADY EXISTS!")
}

bcrypt.hash(body.user_password, null, null, (err, hash) => {
    user.user_password = hash;

    user.save((err, userStored) => {
        if(userStored) return userStored;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

27
28
29
30
31
32
33
34
35
36
37
};*/


// async version of hashing function
/*var myHasher = function(password, tempUserData, insertTempUser, callback) {
  bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(password, salt, function(err, hash) {
     console.log("Temp user password is -"+hash);

      return insertTempUser(hash, tempUserData, callback);
    });
fork icon0
star icon0
watch icon1

241
242
243
244
245
246
247
248
249
250
251
252
    dateTime.setMinutes(time);
    return dateTime;
}


function cryptData(data, callback) {
    bcrypt.hash(data, null, null, callback);
}


function compareCryptData(data, hash, callback) {
    bcrypt.compare(data, hash, callback);
fork icon0
star icon0
watch icon1

110
111
112
113
114
115
116
117
118
119
    return player.call("showNotification", ["Error on database."]);

if(res.length > 0)
    return player.call("showNotification", ["This account already exist"]);

bcrypt.hash(loginPass, null, null, function(err, hash) {
    if(!err)
    {
        gm.mysql.handle.query('INSERT INTO `accounts` SET username = ?, password = ?, email = ?', [loginName, hash, playerEmail], function(err2, res2){
            if(err2)
fork icon0
star icon0
watch icon1

+ 4 other calls in file

11
12
13
14
15
16
17
18
19
20
    });
  });
},
hashAsync: function(data, salt) {
  return new BluePromise(function(resolve, reject) {
    bcrypt.hash(data, salt, noop, function(error, hashedValue) {
      if (error) {
        reject(error);
      }
      resolve(hashedValue);
fork icon0
star icon0
watch icon2