Created
November 29, 2019 17:55
-
-
Save bhrott/ddf31ef712ac15c8ef0deaea50559306 to your computer and use it in GitHub Desktop.
Sample Nodejs bcrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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