Skip to content

Instantly share code, notes, and snippets.

@dthtvwls
Created June 4, 2016 04:10
Show Gist options
  • Save dthtvwls/5dbb6d02ce14c06ef6d54d2cd22b28b7 to your computer and use it in GitHub Desktop.
Save dthtvwls/5dbb6d02ce14c06ef6d54d2cd22b28b7 to your computer and use it in GitHub Desktop.
// 32 16 16 16 48
// 097d43fb e23d 4633 9608 e930f252d32e
// time_low(8) - time_mid(4) - time_hi_and_version(4) - clock_seq_hi_and_reserved(2)/clock_seq_low(2) - node(12)
// leftmost bits are 0100 leftmost bits are 10
var uuid = function () {
var a = new Uint8Array(16); // 16 8-bit octets that are represented by 2-digit hex numbers
crypto.getRandomValues(a);
a[6] = a[6] & 0x0f | 0x40;
a[8] = a[8] & 0x3f | 0x80;
var f = function () {
var array = this;
return [].map.call(arguments, function (index) {
return ('00' + array[index].toString(16)).slice(-2);
}).join('');
}.bind(a);
return f(0, 1, 2, 3) + '-' + f(4, 5) + '-' + f(6, 7) + '-' + f(8, 9) + '-' + f(10, 11, 12, 13, 14, 15);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment