Skip to content

Instantly share code, notes, and snippets.

@fredyfx
Created September 7, 2017 23:32
Show Gist options
  • Save fredyfx/46433f2d3d887a138c95cc4b829ee7f4 to your computer and use it in GitHub Desktop.
Save fredyfx/46433f2d3d887a138c95cc4b829ee7f4 to your computer and use it in GitHub Desktop.
Genera Unique Identifiers en javascript. Extraído desde: https://stackoverflow.com/a/2117523/3613462
//Update, 2017-06-28: A good article from Chrome developers discussing the state of Math.random PRNG quality in Chrome, Firefox, and Safari. tl;dr - As of late-2015 it's "pretty good", but not cryptographic quality. To address that issue, here's an updated version of the above solution that uses ES6, the crypto API, and a bit of JS wizardy I can't take credit for:
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
console.log(uuidv4());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment