Using SHA-256 with NodeJS Crypto

Solution 1:

base64:

const hash = crypto.createHash('sha256').update(pwd).digest('base64');

hex:

crypto.createHash('sha256').update(pwd).digest('hex');

Solution 2:

you can use, like this, in here create a reset token (resetToken), this token is used to create a hex version.in database, you can store hex version.

// Generate token
 const resetToken = crypto.randomBytes(20).toString('hex');
// Hash token and set to resetPasswordToken field
this.resetPasswordToken = crypto
    .createHash('sha256')
    .update(resetToken)
    .digest('hex');

console.log(resetToken )

Solution 3:

nodejs (8) ref

const crypto = require('crypto');
const hash = crypto.createHash('sha256');

hash.on('readable', () => {
    const data = hash.read();
    if (data) {
        console.log(data.toString('hex'));
        // Prints:
        //  6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
    }
});

hash.write('some data to hash');
hash.end();