uuid 3.0.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ // Adapted from Chris Veness' SHA1 code at
2
+ // http://www.movable-type.co.uk/scripts/sha1.html
3
+ 'use strict';
4
+
5
+ function f(s, x, y, z) {
6
+ switch (s) {
7
+ case 0: return (x & y) ^ (~x & z);
8
+ case 1: return x ^ y ^ z;
9
+ case 2: return (x & y) ^ (x & z) ^ (y & z);
10
+ case 3: return x ^ y ^ z;
11
+ }
12
+ }
13
+
14
+ function ROTL(x, n) {
15
+ return (x << n) | (x>>> (32 - n));
16
+ }
17
+
18
+ function sha1(bytes) {
19
+ var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
20
+ var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
21
+
22
+ if (typeof(bytes) == 'string') {
23
+ var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
24
+ bytes = new Array(msg.length);
25
+ for (var i = 0; i < msg.length; i++) bytes[i] = msg.charCodeAt(i);
26
+ }
27
+
28
+ bytes.push(0x80);
29
+
30
+ var l = bytes.length/4 + 2;
31
+ var N = Math.ceil(l/16);
32
+ var M = new Array(N);
33
+
34
+ for (var i=0; i<N; i++) {
35
+ M[i] = new Array(16);
36
+ for (var j=0; j<16; j++) {
37
+ M[i][j] =
38
+ bytes[i * 64 + j * 4] << 24 |
39
+ bytes[i * 64 + j * 4 + 1] << 16 |
40
+ bytes[i * 64 + j * 4 + 2] << 8 |
41
+ bytes[i * 64 + j * 4 + 3];
42
+ }
43
+ }
44
+
45
+ M[N - 1][14] = ((bytes.length - 1) * 8) /
46
+ Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]);
47
+ M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff;
48
+
49
+ for (var i=0; i<N; i++) {
50
+ var W = new Array(80);
51
+
52
+ for (var t=0; t<16; t++) W[t] = M[i][t];
53
+ for (var t=16; t<80; t++) {
54
+ W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
55
+ }
56
+
57
+ var a = H[0];
58
+ var b = H[1];
59
+ var c = H[2];
60
+ var d = H[3];
61
+ var e = H[4];
62
+
63
+ for (var t=0; t<80; t++) {
64
+ var s = Math.floor(t/20);
65
+ var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
66
+ e = d;
67
+ d = c;
68
+ c = ROTL(b, 30) >>> 0;
69
+ b = a;
70
+ a = T;
71
+ }
72
+
73
+ H[0] = (H[0] + a) >>> 0;
74
+ H[1] = (H[1] + b) >>> 0;
75
+ H[2] = (H[2] + c) >>> 0;
76
+ H[3] = (H[3] + d) >>> 0;
77
+ H[4] = (H[4] + e) >>> 0;
78
+ }
79
+
80
+ return [
81
+ H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff,
82
+ H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff,
83
+ H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff,
84
+ H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff,
85
+ H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff
86
+ ];
87
+ }
88
+
89
+ module.exports = sha1;
package/lib/sha1.js ADDED
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+
5
+ function sha1(bytes) {
6
+ if (typeof Buffer.from === 'function') {
7
+ // Modern Buffer API
8
+ if (Array.isArray(bytes)) {
9
+ bytes = Buffer.from(bytes);
10
+ } else if (typeof bytes === 'string') {
11
+ bytes = Buffer.from(bytes, 'utf8');
12
+ }
13
+ } else {
14
+ // Pre-v4 Buffer API
15
+ if (Array.isArray(bytes)) {
16
+ bytes = new Buffer(bytes);
17
+ } else if (typeof bytes === 'string') {
18
+ bytes = new Buffer(bytes, 'utf8');
19
+ }
20
+ }
21
+
22
+ return crypto.createHash('sha1').update(bytes).digest();
23
+ }
24
+
25
+ module.exports = sha1;
package/lib/v35.js ADDED
@@ -0,0 +1,53 @@
1
+ var bytesToUuid = require('./bytesToUuid');
2
+
3
+ function uuidToBytes(uuid) {
4
+ // Note: We assume we're being passed a valid uuid string
5
+ var bytes = [];
6
+ uuid.replace(/[a-fA-F0-9]{2}/g, function(hex) {
7
+ bytes.push(parseInt(hex, 16));
8
+ });
9
+
10
+ return bytes;
11
+ }
12
+
13
+ function stringToBytes(str) {
14
+ str = unescape(encodeURIComponent(str)); // UTF8 escape
15
+ var bytes = new Array(str.length);
16
+ for (var i = 0; i < str.length; i++) {
17
+ bytes[i] = str.charCodeAt(i);
18
+ }
19
+ return bytes;
20
+ }
21
+
22
+ module.exports = function(name, version, hashfunc) {
23
+ var generateUUID = function(value, namespace, buf, offset) {
24
+ var off = buf && offset || 0;
25
+
26
+ if (typeof(value) == 'string') value = stringToBytes(value);
27
+ if (typeof(namespace) == 'string') namespace = uuidToBytes(namespace);
28
+
29
+ if (!Array.isArray(value)) throw TypeError('value must be an array of bytes');
30
+ if (!Array.isArray(namespace) || namespace.length !== 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values');
31
+
32
+ // Per 4.3
33
+ var bytes = hashfunc(namespace.concat(value));
34
+ bytes[6] = (bytes[6] & 0x0f) | version;
35
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
36
+
37
+ if (buf) {
38
+ for (var idx = 0; idx < 16; ++idx) {
39
+ buf[off+idx] = bytes[idx];
40
+ }
41
+ }
42
+
43
+ return buf || bytesToUuid(bytes);
44
+ };
45
+
46
+ generateUUID.name = name;
47
+
48
+ // Pre-defined namespaces, per Appendix C
49
+ generateUUID.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
50
+ generateUUID.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
51
+
52
+ return generateUUID;
53
+ };
package/package.json CHANGED
@@ -1,28 +1,36 @@
1
1
  {
2
2
  "name": "uuid",
3
- "version": "3.0.0",
4
- "description": "RFC4122 (v1 and v4) generator",
3
+ "version": "3.2.1",
4
+ "description": "RFC4122 (v1, v4, and v5) UUIDs",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "guid",
8
8
  "rfc4122"
9
9
  ],
10
10
  "license": "MIT",
11
- "main": "./uuid.js",
12
11
  "bin": {
13
12
  "uuid": "./bin/uuid"
14
13
  },
15
14
  "devDependencies": {
16
- "mocha": "3.1.2"
15
+ "eslint": "4.5.0",
16
+ "mocha": "3.1.2",
17
+ "runmd": "1.0.1",
18
+ "standard-version": "4.2.0"
17
19
  },
18
20
  "scripts": {
19
- "test": "mocha test/test.js"
21
+ "test": "mocha test/test.js",
22
+ "md": "runmd --watch --output=README.md README_js.md",
23
+ "release": "standard-version",
24
+ "prepare": "runmd --output=README.md README_js.md"
20
25
  },
21
26
  "browser": {
22
- "./lib/rng.js": "./lib/rng-browser.js"
27
+ "./lib/rng.js": "./lib/rng-browser.js",
28
+ "./lib/sha1.js": "./lib/sha1-browser.js",
29
+ "./lib/md5.js": "./lib/md5-browser.js"
23
30
  },
24
31
  "repository": {
25
32
  "type": "git",
26
33
  "url": "https://github.com/kelektiv/node-uuid.git"
27
- }
28
- }
34
+ },
35
+ "dependencies": {}
36
+ }
@@ -1,48 +1,17 @@
1
- // Unique ID creation requires a high quality random # generator. We feature
2
- // detect to determine the best RNG source, normalizing to a function that
3
- // returns 128-bits of randomness, since that's what's usually required
4
- var _rng = require('./lib/rng');
5
-
6
- // Maps for number <-> hex string conversion
7
- var _byteToHex = [];
8
- var _hexToByte = {};
9
- for (var i = 0; i < 256; ++i) {
10
- _byteToHex[i] = (i + 0x100).toString(16).substr(1);
11
- _hexToByte[_byteToHex[i]] = i;
12
- }
13
-
14
- function buff_to_string(buf, offset) {
15
- var i = offset || 0;
16
- var bth = _byteToHex;
17
- return bth[buf[i++]] + bth[buf[i++]] +
18
- bth[buf[i++]] + bth[buf[i++]] + '-' +
19
- bth[buf[i++]] + bth[buf[i++]] + '-' +
20
- bth[buf[i++]] + bth[buf[i++]] + '-' +
21
- bth[buf[i++]] + bth[buf[i++]] + '-' +
22
- bth[buf[i++]] + bth[buf[i++]] +
23
- bth[buf[i++]] + bth[buf[i++]] +
24
- bth[buf[i++]] + bth[buf[i++]];
25
- }
1
+ var rng = require('./lib/rng');
2
+ var bytesToUuid = require('./lib/bytesToUuid');
26
3
 
27
4
  // **`v1()` - Generate time-based UUID**
28
5
  //
29
6
  // Inspired by https://github.com/LiosK/UUID.js
30
7
  // and http://docs.python.org/library/uuid.html
31
8
 
32
- // random #'s we need to init node and clockseq
33
- var _seedBytes = _rng();
34
-
35
- // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
36
- var _nodeId = [
37
- _seedBytes[0] | 0x01,
38
- _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
39
- ];
40
-
41
- // Per 4.2.2, randomize (14 bit) clockseq
42
- var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
9
+ var _nodeId;
10
+ var _clockseq;
43
11
 
44
12
  // Previous uuid creation time
45
- var _lastMSecs = 0, _lastNSecs = 0;
13
+ var _lastMSecs = 0;
14
+ var _lastNSecs = 0;
46
15
 
47
16
  // See https://github.com/broofa/node-uuid for API details
48
17
  function v1(options, buf, offset) {
@@ -50,9 +19,27 @@ function v1(options, buf, offset) {
50
19
  var b = buf || [];
51
20
 
52
21
  options = options || {};
53
-
22
+ var node = options.node || _nodeId;
54
23
  var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
55
24
 
25
+ // node and clockseq need to be initialized to random values if they're not
26
+ // specified. We do this lazily to minimize issues related to insufficient
27
+ // system entropy. See #189
28
+ if (node == null || clockseq == null) {
29
+ var seedBytes = rng();
30
+ if (node == null) {
31
+ // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
32
+ node = _nodeId = [
33
+ seedBytes[0] | 0x01,
34
+ seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
35
+ ];
36
+ }
37
+ if (clockseq == null) {
38
+ // Per 4.2.2, randomize (14 bit) clockseq
39
+ clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
40
+ }
41
+ }
42
+
56
43
  // UUID timestamps are 100 nano-second units since the Gregorian epoch,
57
44
  // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
58
45
  // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
@@ -112,46 +99,11 @@ function v1(options, buf, offset) {
112
99
  b[i++] = clockseq & 0xff;
113
100
 
114
101
  // `node`
115
- var node = options.node || _nodeId;
116
102
  for (var n = 0; n < 6; ++n) {
117
103
  b[i + n] = node[n];
118
104
  }
119
105
 
120
- return buf ? buf : buff_to_string(b);
106
+ return buf ? buf : bytesToUuid(b);
121
107
  }
122
108
 
123
- // **`v4()` - Generate random UUID**
124
-
125
- // See https://github.com/broofa/node-uuid for API details
126
- function v4(options, buf, offset) {
127
- // Deprecated - 'format' argument, as supported in v1.2
128
- var i = buf && offset || 0;
129
-
130
- if (typeof(options) == 'string') {
131
- buf = options == 'binary' ? new Array(16) : null;
132
- options = null;
133
- }
134
- options = options || {};
135
-
136
- var rnds = options.random || (options.rng || _rng)();
137
-
138
- // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
139
- rnds[6] = (rnds[6] & 0x0f) | 0x40;
140
- rnds[8] = (rnds[8] & 0x3f) | 0x80;
141
-
142
- // Copy bytes to buffer, if provided
143
- if (buf) {
144
- for (var ii = 0; ii < 16; ++ii) {
145
- buf[i + ii] = rnds[ii];
146
- }
147
- }
148
-
149
- return buf || buff_to_string(rnds);
150
- }
151
-
152
- // Export public API
153
- var uuid = v4;
154
- uuid.v1 = v1;
155
- uuid.v4 = v4;
156
-
157
- module.exports = uuid;
109
+ module.exports = v1;
package/v3.js ADDED
@@ -0,0 +1,4 @@
1
+ var v35 = require('./lib/v35.js');
2
+ var md5 = require('./lib/md5');
3
+
4
+ module.exports = v35('v3', 0x30, md5);
package/v4.js ADDED
@@ -0,0 +1,29 @@
1
+ var rng = require('./lib/rng');
2
+ var bytesToUuid = require('./lib/bytesToUuid');
3
+
4
+ function v4(options, buf, offset) {
5
+ var i = buf && offset || 0;
6
+
7
+ if (typeof(options) == 'string') {
8
+ buf = options === 'binary' ? new Array(16) : null;
9
+ options = null;
10
+ }
11
+ options = options || {};
12
+
13
+ var rnds = options.random || (options.rng || rng)();
14
+
15
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
16
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
17
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
18
+
19
+ // Copy bytes to buffer, if provided
20
+ if (buf) {
21
+ for (var ii = 0; ii < 16; ++ii) {
22
+ buf[i + ii] = rnds[ii];
23
+ }
24
+ }
25
+
26
+ return buf || bytesToUuid(rnds);
27
+ }
28
+
29
+ module.exports = v4;
package/v5.js ADDED
@@ -0,0 +1,3 @@
1
+ var v35 = require('./lib/v35.js');
2
+ var sha1 = require('./lib/sha1');
3
+ module.exports = v35('v5', 0x50, sha1);
package/.npmignore DELETED
@@ -1,2 +0,0 @@
1
- node_modules
2
- .DS_Store
package/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "4"
4
- - "6"
package/HISTORY.md DELETED
@@ -1,24 +0,0 @@
1
- # 3.0.0 (2016-11-17)
2
-
3
- * remove .parse and .unparse
4
-
5
- # 2.0.0
6
-
7
- * Removed uuid.BufferClass
8
-
9
- # 1.4.0
10
-
11
- * Improved module context detection
12
- * Removed public RNG functions
13
-
14
- # 1.3.2
15
-
16
- * Improve tests and handling of v1() options (Issue #24)
17
- * Expose RNG option to allow for perf testing with different generators
18
-
19
- # 1.3.0
20
-
21
- * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)!
22
- * Support for node.js crypto API
23
- * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code
24
-
package/test/mocha.opts DELETED
@@ -1,3 +0,0 @@
1
- --ui qunit
2
- --reporter spec
3
- --check-leaks
package/test/test.js DELETED
@@ -1,96 +0,0 @@
1
- var assert = require('assert');
2
-
3
- var uuid = require('../');
4
-
5
- // Verify ordering of v1 ids created with explicit times
6
- var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
7
-
8
- function compare(name, ids) {
9
- test(name, function() {
10
- // avoid .map for older browsers
11
- for (var i=0 ; i<ids.length ; ++i) {
12
- ids[i] = ids[i].split('-').reverse().join('-');
13
- }
14
- ids = ids.sort();
15
- var sorted = ([].concat(ids)).sort();
16
-
17
- assert(sorted.toString() == ids.toString(), name + ' have expected order');
18
- });
19
- }
20
-
21
- // Verify ordering of v1 ids created using default behavior
22
- compare('uuids with current time', [
23
- uuid.v1(),
24
- uuid.v1(),
25
- uuid.v1(),
26
- uuid.v1(),
27
- uuid.v1()
28
- ]);
29
-
30
- // Verify ordering of v1 ids created with explicit times
31
- compare('uuids with time option', [
32
- uuid.v1({msecs: TIME - 10*3600*1000}),
33
- uuid.v1({msecs: TIME - 1}),
34
- uuid.v1({msecs: TIME}),
35
- uuid.v1({msecs: TIME + 1}),
36
- uuid.v1({msecs: TIME + 28*24*3600*1000})
37
- ]);
38
-
39
- test('msec', function() {
40
- assert(
41
- uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
42
- 'IDs created at same msec are different'
43
- );
44
- });
45
-
46
- test('exception thrown when > 10k ids created in 1ms', function() {
47
- // Verify throw if too many ids created
48
- var thrown = false;
49
- try {
50
- uuid.v1({msecs: TIME, nsecs: 10000});
51
- } catch (e) {
52
- thrown = true;
53
- }
54
- assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
55
- });
56
-
57
- test('clock regression by msec', function() {
58
- // Verify clock regression bumps clockseq
59
- var uidt = uuid.v1({msecs: TIME});
60
- var uidtb = uuid.v1({msecs: TIME - 1});
61
- assert(
62
- parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
63
- 'Clock regression by msec increments the clockseq'
64
- );
65
- });
66
-
67
- test('clock regression by nsec', function() {
68
- // Verify clock regression bumps clockseq
69
- var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
70
- var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
71
- assert(
72
- parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
73
- 'Clock regression by nsec increments the clockseq'
74
- );
75
- });
76
-
77
- test('explicit options product expected id', function() {
78
- // Verify explicit options produce expected id
79
- var id = uuid.v1({
80
- msecs: 1321651533573,
81
- nsecs: 5432,
82
- clockseq: 0x385c,
83
- node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
84
- });
85
- assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
86
- });
87
-
88
- test('ids spanning 1ms boundary are 100ns apart', function() {
89
- // Verify adjacent ids across a msec boundary are 1 time unit apart
90
- var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
91
- var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
92
-
93
- var before = u0.split('-')[0], after = u1.split('-')[0];
94
- var dt = parseInt(after, 16) - parseInt(before, 16);
95
- assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
96
- });