Skip to content

Instantly share code, notes, and snippets.

@neftaly
Created June 1, 2016 13:51
Show Gist options
  • Save neftaly/45bc43e73d8061404a0734798e71dfd2 to your computer and use it in GitHub Desktop.
Save neftaly/45bc43e73d8061404a0734798e71dfd2 to your computer and use it in GitHub Desktop.
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