Skip to content

Instantly share code, notes, and snippets.

@jed
Created January 8, 2014 21:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/8325181 to your computer and use it in GitHub Desktop.
Save jed/8325181 to your computer and use it in GitHub Desktop.
var crypto = require("crypto")
module.exports = function() {
var bytes = crypto.randomBytes(16)
bytes[6] &= 0x0f // 0000xxxx
bytes[6] += 0x40 // 0100xxxx
bytes[8] &= 0x3f // 00xxxxxx
bytes[8] += 0x80 // 10xxxxxx
return [0, 4, 6, 8, 10].map(map).join("-")
function map(len, i, arr) {
return bytes.slice(len, arr[i + 1]).toString("hex")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment