Skip to content

Instantly share code, notes, and snippets.

@m-2k
Created December 16, 2017 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-2k/2dddce2635cbebf2e96d73e3e8fc9e95 to your computer and use it in GitHub Desktop.
Save m-2k/2dddce2635cbebf2e96d73e3e8fc9e95 to your computer and use it in GitHub Desktop.
UUID (Universally Unique Identifier) Generator. Method lets you get cryptographically strong random values
// Description: fast and strong UUID generator for JavaScript
// Author: https://github.com/m-2k
// License: MIT
let uuid = () => Array.from(window.crypto.getRandomValues(new Uint8Array(16))).reduce((acc, byte, idx) => {
switch(idx) {
case 6: acc += "-4"; break
case 8: acc += "-" + ((byte & 0xF) & 0x3 | 0x8).toString(16); break
case 10: case 4: acc += "-"
default: acc += (byte & 0xF).toString(16)
}
return acc + (byte >>> 4).toString(16) }, '' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment