NPM Bcryptjs code example
Example 1: how to hash password in node js
npm i bcrypt
const bcrypt = require('bcrypt');
async function hashIt(password){
const salt = await bcrypt.genSalt(6);
const hashed = await bcrypt.hash(password, salt);
}
hashIt(password);
async function compareIt(password){
const validPassword = await bcrypt.compare(password, hashedPassword);
}
compareIt(password);
Example 2: html-webpack-plugin npm
npm i --save-dev html-webpack-plugin
Example 3: npm bcrypt
npm install bcrypt
Example 4: bcryptjs
npm i bcryptjs
# yarn
yarn add bcryptjs
Example 5: npm bcrypt
const bcrypt = require('bcrypt');
const saltRounds = 10;
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
});
bcrypt.compare(myPlaintextPassword, hash, function(err, result) {
});
Example 6: bcrypt compare hash and password
var bcrypt = dcodeIO.bcrypt;
var salt = bcrypt.genSaltSync(10);
bcrypt.hash('anypassword', salt, (err, res) => {
console.log('hash', res)
hash = res
compare(hash)
});
function compare(encrypted) {
bcrypt.compare('aboveusedpassword', encrypted, (err, res) => {
console.log('Compared result', res, hash)
})
}