Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to get the sha1 hash of a string in Node.js.

In this article, we’ll look at how to get the sha1 hash of a string in Node.js.

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

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

For instance, we write

const crypto = require('crypto')
const shasum = crypto.createHash('sha1')
shasum.update('foo')
const s = shasum.digest('hex')

to call crypto.createHash to create the shasum object.

Then we call shasum.update with the string we want to create the hash from.

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

Conclusion

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

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 *