Created
June 1, 2016 13:51
-
-
Save neftaly/45bc43e73d8061404a0734798e71dfd2 to your computer and use it in GitHub Desktop.
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
const uuid = () => { // Create a secure V4 UUID | |
const randomHexChars = qty => crypto | |
.randomBytes(Math.ceil(qty / 2)) | |
.toString('hex') | |
.slice(0, qty); | |
const reserved = ['8', '9', 'a', 'b'][crypto.randomBytes(1)[0] % 4]; | |
return [ | |
randomHexChars(8), | |
randomHexChars(4), | |
'4' + randomHexChars(3), | |
reserved + randomHexChars(3), | |
randomHexChars(12) | |
].join('-'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment