Categories
JavaScript Answers

How to salt and hash password in Node with crypto?

Spread the love

To salt and hash password in Node with crypto, we call the hashSync method.

For instance, we write

const bcrypt = require("bcrypt");
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("B4c0//", salt);

to create a salt with genSaltSync.

Then we call hashSync with the string to create a hash for and the salt to create the hash.

We then compare the hash with

const isSame = bcrypt.compareSync("B4c0//", hash);

by calling compareSync with the unhashed string with the hash.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.