Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created November 22, 2015 04:31
Show Gist options
  • Save jhurliman/787584eede25a39a815e to your computer and use it in GitHub Desktop.
Save jhurliman/787584eede25a39a815e to your computer and use it in GitHub Desktop.
Generate V4 UUIDs in node.js
function uuid() {
var bytes = require('crypto').randomBytes(16);
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
var str = bytes.toString('hex');
return str.substr(0, 8) + '-' +
str.substr(8, 4) + '-' +
str.substr(12, 4) + '-' +
str.substr(16, 4) + '-' +
str.substr(20, 12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment