bcryptjs npm code example

Example 1: npm bcrypt

npm install bcrypt

Example 2: bcryptjs

npm i bcryptjs

# yarn
yarn add bcryptjs

Example 3: install bcrypt

>> npm install bcrypt

const bcrypt = require('bcrypt');

Example 4: install bcrypt

npm install bcrypt

Example 5: install bcrypt

npm install bcryptjs

Example 6: bcrypt compare hash and password

var bcrypt = dcodeIO.bcrypt; 

    /** One way, can't decrypt but can compare */
    var salt = bcrypt.genSaltSync(10);

    /** Encrypt password */
    bcrypt.hash('anypassword', salt, (err, res) => {
        console.log('hash', res)
        hash = res
        compare(hash)
    });

    /** Compare stored password with new encrypted password */
    function compare(encrypted) {
        bcrypt.compare('aboveusedpassword', encrypted, (err, res) => {
            // res == true or res == false
            console.log('Compared result', res, hash) 
        })
    }

// If u want do the same with NodeJS use this:
/*		var bcrypt = require('bcryptjs')	*/

Tags:

Java Example