xxxxxxxxxx
const bcrypt = require('bcrypt');
// Plain text password to be hashed
const plainPassword = 'myPassword123';
// Generate a salt
const saltRounds = 10;
const salt = bcrypt.genSaltSync(saltRounds);
// Hash the password using the generated salt
const hashedPassword = bcrypt.hashSync(plainPassword, salt);
console.log('Password:', plainPassword);
console.log('Hashed Password:', hashedPassword);
// Comparing a password with the hashed password
const isPasswordMatch = bcrypt.compareSync(plainPassword, hashedPassword);
console.log('Is Password Match:', isPasswordMatch);
xxxxxxxxxx
// This is probably the easiest way to use bcryptjs in nodejs
const bcrypt = require("bcryptjs")
const password = "123456"
bcrypt.hash(password, 10)
.then('''do something''')
.catch(err => console.log(err))
xxxxxxxxxx
// Hash Password
const hashedPassword = await bcrypt.hash(req.body.password, 10)
//compare password
let password = await bcrypt.compare(req.body.password,user.password)
if(!password){
return next(CustomErrorHandler.wrongCredential())
}
xxxxxxxxxx
// npm bcrypt used salt
// code
const bcrypt = require('bcrypt');
// hashing password.(registration)
const hashedPassword = await bcrypt.hash(req.body.password,10);
const user = { name: req.body.username, password: hashedPassword }
// compare password (login)
try {
if (await bcrypt.compare(password, user.password)) {
console.log("login successfull");
} else {
console.log("login failed");
}
} catch (e) {
console.log("something went wrong", error);
}
re.send(user)
xxxxxxxxxx
var bcrypt = require('bcryptjs');
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync("B4c0/\/", salt);
// Store hash in your password DB.
xxxxxxxxxx
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync(myPlaintextPassword, salt);
// Store hash in your password DB.
xxxxxxxxxx
>>> import bcrypt
>>> password = b"super secret password"
>>> # Hash a password for the first time, with a randomly-generated salt
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt())
>>> # Check that an unhashed password matches one that has previously been
>>> # hashed
>>> if bcrypt.checkpw(password, hashed):
print("It Matches!")
else:
print("It Does not Match :(")