-
-
Save dcts/a40e4ae321ed77e6a7fe6920d950bee0 to your computer and use it in GitHub Desktop.
UUID v4 JavaScript implementation with window.crypto.getRandomValues()
This file contains hidden or 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
function uuid () { | |
function getRandomSymbol (symbol) { | |
var array; | |
if (symbol === 'y') { | |
array = ['8', '9', 'a', 'b']; | |
return array[Math.floor(Math.random() * array.length)]; | |
} | |
array = new Uint8Array(1); | |
window.crypto.getRandomValues(array); | |
return (array[0] % 16).toString(16); | |
} | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, getRandomSymbol); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment