uuid 3.0.1 → 3.3.0

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,56 @@
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
+ // only attempt to set the name if's configurable (which it should be on node > 0.12)
47
+ if (Object.getOwnPropertyDescriptor(generateUUID, 'name').configurable) {
48
+ Object.defineProperty(generateUUID, 'name', {value: name});
49
+ }
50
+
51
+ // Pre-defined namespaces, per Appendix C
52
+ generateUUID.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
53
+ generateUUID.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
54
+
55
+ return generateUUID;
56
+ };
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "uuid",
3
- "version": "3.0.1",
4
- "description": "RFC4122 (v1 and v4) generator",
3
+ "version": "3.3.0",
4
+ "description": "RFC4122 (v1, v4, and v5) UUIDs",
5
+ "commitlint": {
6
+ "extends": [
7
+ "@commitlint/config-conventional"
8
+ ]
9
+ },
5
10
  "keywords": [
6
11
  "uuid",
7
12
  "guid",
@@ -12,16 +17,28 @@
12
17
  "uuid": "./bin/uuid"
13
18
  },
14
19
  "devDependencies": {
15
- "mocha": "3.1.2"
20
+ "@commitlint/cli": "7.0.0",
21
+ "@commitlint/config-conventional": "7.0.1",
22
+ "eslint": "4.19.1",
23
+ "husky": "0.14.3",
24
+ "mocha": "5.2.0",
25
+ "runmd": "1.0.1",
26
+ "standard-version": "4.4.0"
16
27
  },
17
28
  "scripts": {
18
- "test": "mocha test/test.js"
29
+ "commitmsg": "commitlint -E GIT_PARAMS",
30
+ "test": "mocha test/test.js",
31
+ "md": "runmd --watch --output=README.md README_js.md",
32
+ "release": "standard-version",
33
+ "prepare": "runmd --output=README.md README_js.md"
19
34
  },
20
35
  "browser": {
21
- "./lib/rng.js": "./lib/rng-browser.js"
36
+ "./lib/rng.js": "./lib/rng-browser.js",
37
+ "./lib/sha1.js": "./lib/sha1-browser.js",
38
+ "./lib/md5.js": "./lib/md5-browser.js"
22
39
  },
23
40
  "repository": {
24
41
  "type": "git",
25
42
  "url": "https://github.com/kelektiv/node-uuid.git"
26
43
  }
27
- }
44
+ }
package/v1.js CHANGED
@@ -1,6 +1,3 @@
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
1
  var rng = require('./lib/rng');
5
2
  var bytesToUuid = require('./lib/bytesToUuid');
6
3
 
@@ -9,20 +6,12 @@ var bytesToUuid = require('./lib/bytesToUuid');
9
6
  // Inspired by https://github.com/LiosK/UUID.js
10
7
  // and http://docs.python.org/library/uuid.html
11
8
 
12
- // random #'s we need to init node and clockseq
13
- var _seedBytes = rng();
14
-
15
- // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
16
- var _nodeId = [
17
- _seedBytes[0] | 0x01,
18
- _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
19
- ];
20
-
21
- // Per 4.2.2, randomize (14 bit) clockseq
22
- var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
9
+ var _nodeId;
10
+ var _clockseq;
23
11
 
24
12
  // Previous uuid creation time
25
- var _lastMSecs = 0, _lastNSecs = 0;
13
+ var _lastMSecs = 0;
14
+ var _lastNSecs = 0;
26
15
 
27
16
  // See https://github.com/broofa/node-uuid for API details
28
17
  function v1(options, buf, offset) {
@@ -30,9 +19,27 @@ function v1(options, buf, offset) {
30
19
  var b = buf || [];
31
20
 
32
21
  options = options || {};
33
-
22
+ var node = options.node || _nodeId;
34
23
  var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
35
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
+
36
43
  // UUID timestamps are 100 nano-second units since the Gregorian epoch,
37
44
  // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
38
45
  // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
@@ -92,7 +99,6 @@ function v1(options, buf, offset) {
92
99
  b[i++] = clockseq & 0xff;
93
100
 
94
101
  // `node`
95
- var node = options.node || _nodeId;
96
102
  for (var n = 0; n < 6; ++n) {
97
103
  b[i + n] = node[n];
98
104
  }
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 CHANGED
@@ -5,7 +5,7 @@ function v4(options, buf, offset) {
5
5
  var i = buf && offset || 0;
6
6
 
7
7
  if (typeof(options) == 'string') {
8
- buf = options == 'binary' ? new Array(16) : null;
8
+ buf = options === 'binary' ? new Array(16) : null;
9
9
  options = null;
10
10
  }
11
11
  options = options || {};
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,8 +0,0 @@
1
- node_modules
2
- .DS_Store
3
-
4
- # VIM temp files
5
- *.sw*
6
-
7
- # Mac desktop services store
8
- .DS_Store
package/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.12"
4
- - "4"
5
- - "6"
package/HISTORY.md DELETED
@@ -1,28 +0,0 @@
1
- # 3.0.1 (2016-11-28)
2
-
3
- * split uuid versions into separate files
4
-
5
- # 3.0.0 (2016-11-17)
6
-
7
- * remove .parse and .unparse
8
-
9
- # 2.0.0
10
-
11
- * Removed uuid.BufferClass
12
-
13
- # 1.4.0
14
-
15
- * Improved module context detection
16
- * Removed public RNG functions
17
-
18
- # 1.3.2
19
-
20
- * Improve tests and handling of v1() options (Issue #24)
21
- * Expose RNG option to allow for perf testing with different generators
22
-
23
- # 1.3.0
24
-
25
- * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)!
26
- * Support for node.js crypto API
27
- * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code
28
-
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
- });