How to sha1 hash with Node.js

March 28, 2021

You don't need to install external packages to hash a string using the SHA-1 hashing method.

Node.js comes with a built-in package called crypto that you can use. Here's a simple way:

const crypto = require('crypto')

const str = 'This is the string I want to hash.'
const hash = crypto.createHash('sha1').update(str).digest('hex')

The resulting hash is str, hashed using SHA-1 to create a shorter string that is fairly unique to the original string.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!

Loading...