Notes

Secure Password Hashing

Using Bcrypt to Hash Passwords

What is Cryptography?

What is encryption?

How does encryption work?

When is it appropriate to use encryption?

What is hashing?

When is it appropriate to use hashing?

Using Bcrypt to hash user passwords

const saltRounds = 10;
const hash = await bcrypt.hash(password, saltRounds);
const isPassword = await bcrypt.compare(password, hash);
const bcrypt = require("bcryptjs");

async function getHash(password, saltRounds) {
  const hash = await bcrypt.hash(password, saltRounds);
  console.log(hash);
  return hash;
}

async function isPassword(password, hash) {
  const isPassword = await bcrypt.compare(password, hash);
  console.log(isPassword);
  return isPassword;
}

(async () => {
  const hashedPassword = await getHash("P@ssw0rd", 10);
  const passwordIsMatch = await isPassword("P@ssw0rd", hashedPassword);
})();
const saltRounds = 10;
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync("B4c0//", salt);

bcrypt.compareSync("B4c0//", hash); // true
bcrypt.compareSync("not_bacon", hash); // false