Skip to content

Instantly share code, notes, and snippets.

@bhrott
Created November 29, 2019 17:55
Show Gist options
  • Save bhrott/ddf31ef712ac15c8ef0deaea50559306 to your computer and use it in GitHub Desktop.
Save bhrott/ddf31ef712ac15c8ef0deaea50559306 to your computer and use it in GitHub Desktop.
Sample Nodejs bcrypt
const bcrypt = require("bcrypt")
const SALT_ROUNDS = 10
async function hash(password) {
const salt = await bcrypt.genSalt(SALT_ROUNDS)
const hashed = await bcrypt.hash(password, salt)
return hashed
}
async function compare(password, hashed) {
const match = await bcrypt.compare(password, hashed)
return match
}
async function run() {
const password = 'test123'
const hashed = await hash(password)
const match = await compare(password, hashed)
console.log(`Password: ${password}`)
console.log(`Hashed: ${hashed}`)
console.log(`Match: ${match}`)
}
run()
{
"name": "sample-bcrypt",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment