Categories
JavaScript Answers

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

Spread the love

To generate random SHA1 hash to use as ID in Node.js, we use the crypto.randomBytes method.

For instance, we write

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

to call randomBytes to create a byte object with 20 bytes.

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

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 *