Created
December 16, 2017 23:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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