uuid.modified.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // uuid.js
  2. //
  3. // Copyright (c) 2010-2012 Robert Kieffer
  4. // MIT License - http://opensource.org/licenses/mit-license.php
  5. /*global window, require, define */
  6. (function(_window) {
  7. 'use strict';
  8. // Unique ID creation requires a high quality random # generator. We feature
  9. // detect to determine the best RNG source, normalizing to a function that
  10. // returns 128-bits of randomness, since that's what's usually required
  11. var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot;
  12. function setupBrowser() {
  13. // Allow for MSIE11 msCrypto
  14. //var _crypto = _window.crypto || _window.msCrypto;
  15. var crypto = {}
  16. var _crypto = crypto || _window.crypto || _window.msCrypto;
  17. if (!_rng && _crypto && _crypto.getRandomValues) {
  18. // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
  19. //
  20. // Moderately fast, high quality
  21. try {
  22. var _rnds8 = new Uint8Array(16);
  23. _whatwgRNG = _rng = function whatwgRNG() {
  24. _crypto.getRandomValues(_rnds8);
  25. return _rnds8;
  26. };
  27. _rng();
  28. } catch (e) {}
  29. }
  30. if (!_rng) {
  31. // Math.random()-based (RNG)
  32. //
  33. // If all else fails, use Math.random(). It's fast, but is of unspecified
  34. // quality.
  35. var _rnds = new Array(16);
  36. _mathRNG = _rng = function() {
  37. for (var i = 0, r; i < 16; i++) {
  38. if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
  39. _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
  40. }
  41. return _rnds;
  42. };
  43. if ('undefined' !== typeof console && console.warn) {
  44. // console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
  45. }
  46. }
  47. }
  48. setupBrowser();
  49. // Buffer class to use
  50. var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
  51. // Maps for number <-> hex string conversion
  52. var _byteToHex = [];
  53. var _hexToByte = {};
  54. for (var i = 0; i < 256; i++) {
  55. _byteToHex[i] = (i + 0x100).toString(16).substr(1);
  56. _hexToByte[_byteToHex[i]] = i;
  57. }
  58. // **`parse()` - Parse a UUID into it's component bytes**
  59. function parse(s, buf, offset) {
  60. var i = (buf && offset) || 0,
  61. ii = 0;
  62. buf = buf || [];
  63. s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
  64. if (ii < 16) { // Don't overflow!
  65. buf[i + ii++] = _hexToByte[oct];
  66. }
  67. });
  68. // Zero out remaining bytes if string was short
  69. while (ii < 16) {
  70. buf[i + ii++] = 0;
  71. }
  72. return buf;
  73. }
  74. // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
  75. function unparse(buf, offset) {
  76. var i = offset || 0,
  77. bth = _byteToHex;
  78. return bth[buf[i++]] + bth[buf[i++]] +
  79. bth[buf[i++]] + bth[buf[i++]] + '-' +
  80. bth[buf[i++]] + bth[buf[i++]] + '-' +
  81. bth[buf[i++]] + bth[buf[i++]] + '-' +
  82. bth[buf[i++]] + bth[buf[i++]] + '-' +
  83. bth[buf[i++]] + bth[buf[i++]] +
  84. bth[buf[i++]] + bth[buf[i++]] +
  85. bth[buf[i++]] + bth[buf[i++]];
  86. }
  87. // **`v1()` - Generate time-based UUID**
  88. //
  89. // Inspired by https://github.com/LiosK/UUID.js
  90. // and http://docs.python.org/library/uuid.html
  91. // random #'s we need to init node and clockseq
  92. var _seedBytes = _rng();
  93. // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
  94. var _nodeId = [
  95. _seedBytes[0] | 0x01,
  96. _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
  97. ];
  98. // Per 4.2.2, randomize (14 bit) clockseq
  99. var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
  100. // Previous uuid creation time
  101. var _lastMSecs = 0,
  102. _lastNSecs = 0;
  103. // See https://github.com/broofa/node-uuid for API details
  104. function v1(options, buf, offset) {
  105. var i = buf && offset || 0;
  106. var b = buf || [];
  107. options = options || {};
  108. var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
  109. // UUID timestamps are 100 nano-second units since the Gregorian epoch,
  110. // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
  111. // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
  112. // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
  113. var msecs = (options.msecs != null) ? options.msecs : new Date().getTime();
  114. // Per 4.2.1.2, use count of uuid's generated during the current clock
  115. // cycle to simulate higher resolution clock
  116. var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;
  117. // Time since last uuid creation (in msecs)
  118. var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs) / 10000;
  119. // Per 4.2.1.2, Bump clockseq on clock regression
  120. if (dt < 0 && options.clockseq == null) {
  121. clockseq = clockseq + 1 & 0x3fff;
  122. }
  123. // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
  124. // time interval
  125. if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
  126. nsecs = 0;
  127. }
  128. // Per 4.2.1.2 Throw error if too many uuids are requested
  129. if (nsecs >= 10000) {
  130. throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
  131. }
  132. _lastMSecs = msecs;
  133. _lastNSecs = nsecs;
  134. _clockseq = clockseq;
  135. // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  136. msecs += 12219292800000;
  137. // `time_low`
  138. var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  139. b[i++] = tl >>> 24 & 0xff;
  140. b[i++] = tl >>> 16 & 0xff;
  141. b[i++] = tl >>> 8 & 0xff;
  142. b[i++] = tl & 0xff;
  143. // `time_mid`
  144. var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
  145. b[i++] = tmh >>> 8 & 0xff;
  146. b[i++] = tmh & 0xff;
  147. // `time_high_and_version`
  148. b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
  149. b[i++] = tmh >>> 16 & 0xff;
  150. // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
  151. b[i++] = clockseq >>> 8 | 0x80;
  152. // `clock_seq_low`
  153. b[i++] = clockseq & 0xff;
  154. // `node`
  155. var node = options.node || _nodeId;
  156. for (var n = 0; n < 6; n++) {
  157. b[i + n] = node[n];
  158. }
  159. return buf ? buf : unparse(b);
  160. }
  161. // **`v4()` - Generate random UUID**
  162. // See https://github.com/broofa/node-uuid for API details
  163. function v4(options, buf, offset) {
  164. // Deprecated - 'format' argument, as supported in v1.2
  165. var i = buf && offset || 0;
  166. if (typeof(options) === 'string') {
  167. buf = (options === 'binary') ? new BufferClass(16) : null;
  168. options = null;
  169. }
  170. options = options || {};
  171. var rnds = options.random || (options.rng || _rng)();
  172. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  173. rnds[6] = (rnds[6] & 0x0f) | 0x40;
  174. rnds[8] = (rnds[8] & 0x3f) | 0x80;
  175. // Copy bytes to buffer, if provided
  176. if (buf) {
  177. for (var ii = 0; ii < 16; ii++) {
  178. buf[i + ii] = rnds[ii];
  179. }
  180. }
  181. return buf || unparse(rnds);
  182. }
  183. // Export public API
  184. var uuid = v4;
  185. uuid.v1 = v1;
  186. uuid.v4 = v4;
  187. uuid.parse = parse;
  188. uuid.unparse = unparse;
  189. uuid.BufferClass = BufferClass;
  190. uuid._rng = _rng;
  191. uuid._mathRNG = _mathRNG;
  192. uuid._nodeRNG = _nodeRNG;
  193. uuid._whatwgRNG = _whatwgRNG;
  194. if (('undefined' !== typeof module) && module.exports) {
  195. // Publish as node.js module
  196. module.exports = uuid;
  197. } else if (typeof define === 'function' && define.amd) {
  198. // Publish as AMD module
  199. define(function() { return uuid; });
  200. } else {
  201. // Publish as global (in browsers)
  202. _previousRoot = _window.uuid;
  203. // **`noConflict()` - (browser only) to reset global 'uuid' var**
  204. uuid.noConflict = function() {
  205. _window.uuid = _previousRoot;
  206. return uuid;
  207. };
  208. _window.uuid = uuid;
  209. }
  210. })('undefined' !== typeof window ? window : null);