JavaScript hash generator


There are a few ways to generate a hash with JavaScript.

One approach is to use toString() to convert a number to base 36:

(19440).toString(36); // 'f00'

You can generate the number using Math.random():

Math.random().toString(36); // '0.t8422kr67xeufojkb4piizfr'

Or get the current milliseconds since the UNIX epoch:

Date.now().toString(36); // 'iyl0uscb'

However, the above solutions don’t guarantee uniqueness because Math.random could return the same number and Date.now will return the same value if time hasn’t changed.

As a result, I recommend checking out uuid and shortid. Also, there’s a way to generate an MD5 hash with Node.js.