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

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.