/** * Quickly returns a random UUID that is compliant with RFC-4122 Version 4. * * An example result is "2c3fa383-0d9d-4104-8fa2-58fdf614f021" * * Works in modern browsers and IE11+, and not Android Browser. A fallback if * you care about those is to drop the Uint32Array and crypto lines, and * replace the dX = buf[X] lines with dX = Math.random() * 0x100000000 >>> 0. * In node.js do the same but use dX = crypto.randomBytes(4).readUInt32BE(0). * * This is a modified version of this StackOverflow answer by Jeff Ward: * http://stackoverflow.com/a/21963136/843621 * * @license MIT license */ var uuid = (function() { var lut = [], // look-up table to convert decimals (0-256) to hexadecimals (0x00-0xff) buf = new Uint32Array(4); // random value buffer for (var i = 0; i < 256; i++) { lut[i] = (i < 16 ? '0' : '') + (i).toString(16); } return function() { window.crypto.getRandomValues(buf); var d0 = buf[0], d1 = buf[1], d2 = buf[2], d3 = buf[3]; return lut[d0&0xff]+ lut[d0>>8&0xff]+ lut[d0>>16&0xff]+ lut[d0>>24&0xff]+'-'+ lut[d1&0xff]+ lut[d1>>8&0xff]+'-'+lut[d1>>16&0x0f|0x40]+lut[d1>>24&0xff]+'-'+ lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+lut[d2>>16&0xff]+ lut[d2>>24&0xff]+ lut[d3&0xff] +lut[d3>>8&0xff]+ lut[d3>>16&0xff]+ lut[d3>>24&0xff]; }; })();