I'm trying to hash a variable in NodeJS like so:
var crypto = require('crypto'); var hash = crypto.createHash('sha256'); var code = 'bacon'; code = hash.update(code); code = hash.digest(code); console.log(code);
But looks like I have misunderstood the docs as the console.log doesn't log a hashed version of bacon but just some information about SlowBuffer.
What's the correct way to do this?
With cryptography in Node. js, you can hash passwords and store them in the database so that data cannot be converted to plain text after it is hashed; it can only be verified. When malicious actors get ahold of your database, they cannot decode the encrypted information.
Crypto is a module in Node. js which deals with an algorithm that performs data encryption and decryption. This is used for security purpose like user authentication where storing the password in Database in the encrypted form. Crypto module provides set of classes like hash, HMAC, cipher, decipher, sign, and verify.
The crypto. createHash() method will create a hash object and then return it. THis hash object can be used for generating hash digests by using the given algorithm. The optional options are used for controlling the stream behaviour.
base64:
const hash = crypto.createHash('sha256').update(pwd).digest('base64');
hex:
crypto.createHash('sha256').update(pwd).digest('hex');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With