Categories
JavaScript Answers

How to get the sha1 hash of a string in Node.js?

Spread the love

To get the sha1 hash of a string in Node.js, we use the crypto module.

For instance, we write

const crypto = require("crypto");
const shasum = crypto.createHash("sha1");
shasum.update("foo");
const hash = shasum.digest("hex");

to call createHash to create a hash.

Then we call update to add content to it.

Finally, we call digest with 'hex' to return the hash.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *