Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SHA-256 with NodeJS Crypto

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?

like image 499
Cameron Avatar asked Jan 15 '15 18:01

Cameron


People also ask

Is cryptography supported in node JS?

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.

What is crypto in node JS How do you cipher the secure information in node JS?

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.

How do you use crypto createHash?

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.


1 Answers

base64:

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

hex:

crypto.createHash('sha256').update(pwd).digest('hex'); 
like image 55
MaximeF Avatar answered Oct 26 '22 09:10

MaximeF