This is a quick example of how to hash and verify passwords in Node.js using the bcryptjs password hashing library which is a pure JavaScript implementation of the bcrypt password hashing function.

For more info on the bcryptjs password hashing JavaScript library see https://www.npmjs.com/package/bcryptjs.

For more info on the underlying bcrypt password hashing function, see https://en.wikipedia.org/wiki/bcrypt.

Installing bcryptjs from npm

With the npm CLI: npm install bcryptjs

With the yarn CLI: yarn add bcryptjs

Hashing a password in Node.js

This code hashes the password 'Pa$$w0rd' using bcrypt and stores the result in the passwordHash variable.

const passwordHash = bcrypt.hashSync('Pa$$w0rd', 10);

Verify a password against a hash in Node.js

This code verifies the password 'Pa$$w0rd' using bcrypt against the hash stored in the passwordHash variable.

const verified = bcrypt.compareSync('Pa$$w0rd', passwordHash);

#node.js #javascript #security #authentication and authorization

Node.js - Hash and Verify Passwords with Bcrypt
4.15 GEEK