How to use SHA-256 with Node.js crypto?

Sometimes, we want to use SHA-256 with Node.js crypto.

In this article, we’ll look at how to use SHA-256 with Node.js crypto.

How to use SHA-256 with Node.js crypto?

To use SHA-256 with Node.js crypto, we call the createHash method.

For instance, we write

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

to call createHash with 'sha256' and call update with the string we want to creatre the has from to create the hash.

Then we return the hash digest string from the hash with the digest method.

We pass in 'base64' as the argument, so the base64 hash digest is returned.

Also, we can pass in 'hex' to digest to return a hex hash digest.

Conclusion

To use SHA-256 with Node.js crypto, we call the createHash method.