How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

Sometimes, we want to generate random SHA1 hash to use as ID in Node.js and JavaScript.

In this article, we’ll look at how to generate random SHA1 hash to use as ID in Node.js and JavaScript.

How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.

For instance, we write

const id = crypto.randomBytes(20).toString("hex");

to call crypto.randomBytes to create a 20 bytes buffer.

Then we call toString with 'hex' on the bytes buffer to convert it to a hex string.

Conclusion

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.