To create a SHA1 hash from a string in JavaScript, you can use the crypto
module that is available in both Node.js and modern web browsers. Here's an example code snippet:
294 chars12 lines
In this example, we first import the crypto
module and define a function called sha1Hash
that takes a string as input. Inside this function, we create a new SHA1 hash instance using crypto.createHash('sha1')
, update it with the input string using sha1.update(str)
, and finally return the digest in hexadecimal format using sha1.digest('hex')
.
We then use this function to create a SHA1 hash of the "hello world" string and store it in myHash
. Finally, we log the result to the console, which should be 2ef7bde608ce5404e97d5f042f95f89f1c232871
.
gistlibby LogSnag