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.
With the npm CLI: npm install bcryptjs
With the yarn CLI: yarn add bcryptjs
This code hashes the password 'Pa$$w0rd'
using bcrypt and stores the result in the passwordHash
variable.
const passwordHash = bcrypt.hashSync('Pa$$w0rd', 10);
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