uuid-by-string 4.0.0 → 4.1.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.
package/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  uuid-by-string
2
2
  [![NPM](https://img.shields.io/npm/v/uuid-by-string.svg?style=flat-square&maxAge=3600)](https://www.npmjs.com/package/uuid-by-string)
3
3
  [![Downloads](https://img.shields.io/npm/dw/uuid-by-string.svg?style=flat-square&maxAge=3600)](https://www.npmjs.com/package/uuid-by-string)
4
- [![Build Status](https://img.shields.io/travis/danakt/uuid-by-string.svg?style=flat-square&maxAge=3600)](https://travis-ci.org/danakt/uuid-by-string)
5
4
  =======================
6
- Generates the [RFC-4122](https://tools.ietf.org/html/rfc4122#section-4.3) Name-Based UUID. Supports 3 and 5 versions of UUID.
5
+ Generates deterministic name-based UUIDs (versions 3 and 5) from strings. With a namespace, output is fully compliant with [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562#section-5.3) (formerly RFC 4122). Without a namespace, see [Namespaces and RFC compliance](#namespaces-and-rfc-compliance).
7
6
 
8
7
  ## Installation
9
8
 
@@ -28,7 +27,7 @@ const uuidHash = getUuid('Hello world!');
28
27
 
29
28
  The string `Hello world!` will always returns `d3486ae9-136e-5856-bc42-212385ea7970`.
30
29
 
31
- You can specify the UUID version. Available versions is 3 and 5 according to [RFC-4122](https://tools.ietf.org/html/rfc4122#section-4.3). The version is responsible for the hashing algorithm: version 3 uses MD5, and version 5 uses SHA-1. SHA-1 used by default if version is not specified.
30
+ You can specify the UUID version. Available versions are 3 and 5 according to [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562#section-5.3). The version is responsible for the hashing algorithm: version 3 uses MD5, and version 5 uses SHA-1. SHA-1 used by default if version is not specified.
32
31
 
33
32
  ```js
34
33
  const uuidV3Hash = getUuid('Hello world!', 3);
@@ -38,6 +37,31 @@ const uuidV5Hash = getUuid('Hello world!', 5);
38
37
  // d3486ae9-136e-5856-bc42-212385ea7970
39
38
  ```
40
39
 
40
+ ## Namespaces and RFC compliance
41
+
42
+ RFC 9562 (formerly RFC 4122) defines a name-based UUID as the hash of a **namespace UUID followed by the name**. Passing a namespace makes the output fully RFC-compliant and identical to other implementations (Python's `uuid.uuid5`, the `uuid` npm package, etc.). The standard namespaces are available as `getUuid.DNS`, `getUuid.URL`, `getUuid.OID` and `getUuid.X500`, and any other valid UUID can be used as a namespace too:
43
+
44
+ ```js
45
+ getUuid('www.example.com', getUuid.DNS, 5);
46
+ // 2ed6657d-e927-568b-95e1-2665a8aea6a2
47
+ // same as Python's uuid.uuid5(uuid.NAMESPACE_DNS, 'www.example.com')
48
+ ```
49
+
50
+ When the namespace is omitted, **only the name is hashed**. This is not described by the RFC, but it is the long-standing default of this library and is kept for backward compatibility. For version 3 it matches Java's `UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8))`:
51
+
52
+ ```js
53
+ getUuid('Hello world!', 3);
54
+ // 86fb269d-190d-3c85-b6e0-468ceca42a20
55
+ // same as Java's UUID.nameUUIDFromBytes
56
+ ```
57
+
58
+ If you need RFC-compliant output but have no namespace of your own, use the nil UUID (`getUuid.NIL`). Note that this gives **different** results than omitting the namespace:
59
+
60
+ ```js
61
+ getUuid('Hello world!', getUuid.NIL);
62
+ // ec74fa6c-5be6-5388-8cb3-d91001210130
63
+ ```
64
+
41
65
  ## API
42
66
 
43
67
  `getUuid(name [, version]);`
@@ -45,8 +69,10 @@ const uuidV5Hash = getUuid('Hello world!', 5);
45
69
  `getUuid(name [, namespace, version]);`
46
70
 
47
71
  - `name` — hashing target
48
- - `namespace` _Optional_ — UUID namespace
49
- - `version` _Optional_ — 3 or 5, version of UUID
72
+ - `namespace` _Optional_ — namespace UUID, e.g. `getUuid.DNS`. Required for RFC-compliant output, see [Namespaces and RFC compliance](#namespaces-and-rfc-compliance)
73
+ - `version` _Optional_ — 3 or 5, version of UUID. Default is 5
74
+
75
+ Constants: `getUuid.NIL`, `getUuid.DNS`, `getUuid.URL`, `getUuid.OID`, `getUuid.X500`.
50
76
 
51
77
  ## License
52
78
 
package/index.d.ts CHANGED
@@ -1,16 +1,31 @@
1
1
  /**
2
- * Generates the Name-Based UUID v3 and v5 according to RFC-4122
3
- * https://tools.ietf.org/html/rfc4122#section-4.3
2
+ * Generates the Name-Based UUID v3 and v5.
3
+ *
4
+ * With a namespace, output is compliant with RFC 9562 (formerly RFC 4122).
5
+ * Without a namespace, only the name is hashed: this is not described by the
6
+ * RFC, but for v3 it matches Java's `UUID.nameUUIDFromBytes`.
7
+ * https://www.rfc-editor.org/rfc/rfc9562#section-5.3
4
8
  * @param {string} target Hashing target
5
- * @param {string} [namespace] Some name space within which generation occurs
6
- * @param {3|5} [version=3] Version of UUID. Available versions is 3 and 5
7
- * according to RFC-4122. The version is responsible for the hashing algorithm:
8
- * version 3 uses MD5, and version 5 uses SHA-1.
9
+ * @param {string} [namespace] Namespace UUID, e.g. `getUuidByString.DNS`
10
+ * @param {3|5} [version=5] Version of UUID. Available versions are 3 and 5.
11
+ * The version is responsible for the hashing algorithm: version 3 uses MD5,
12
+ * and version 5 uses SHA-1. Default is 5.
9
13
  * @returns {string} UUID
10
14
  */
11
15
  declare function getUuidByString(target: string, namespace?: string, version?: 3 | 5): string
12
16
  declare function getUuidByString(target: string, version?: 3 | 5): string
13
17
 
14
- // prettier-ignore
15
- declare namespace getUuidByString {}
18
+ declare namespace getUuidByString {
19
+ /** Nil UUID: `00000000-0000-0000-0000-000000000000` */
20
+ const NIL: string
21
+ /** Namespace for fully-qualified domain names */
22
+ const DNS: string
23
+ /** Namespace for URLs */
24
+ const URL: string
25
+ /** Namespace for ISO OIDs */
26
+ const OID: string
27
+ /** Namespace for X.500 DNs */
28
+ const X500: string
29
+ }
30
+
16
31
  export = getUuidByString
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "uuid-by-string",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "Generating uuid-hash from string",
5
5
  "main": "src/index.js",
6
6
  "typings": "index.d.ts",
7
+ "files": [
8
+ "src",
9
+ "index.d.ts"
10
+ ],
7
11
  "scripts": {
8
12
  "test": "jest",
9
- "lint": "eslint",
13
+ "lint": "eslint src",
10
14
  "prepublishOnly": "npm run lint && npm run test"
11
15
  },
12
16
  "repository": {
@@ -39,12 +43,10 @@
39
43
  "src/**"
40
44
  ]
41
45
  },
42
- "dependencies": {
43
- "js-md5": "^0.7.3",
44
- "js-sha1": "^0.6.0"
45
- },
46
46
  "devDependencies": {
47
- "eslint": "^8.23.0",
48
- "jest": "^27.0.1"
47
+ "@eslint/js": "^10.0.1",
48
+ "eslint": "^10.16.0",
49
+ "globals": "^17.15.0",
50
+ "jest": "^30.5.2"
49
51
  }
50
52
  }
package/src/index.js CHANGED
@@ -1,16 +1,22 @@
1
1
  var lib = require('./lib');
2
+ var md5 = require('./md5');
3
+ var sha1 = require('./sha1');
2
4
 
3
5
  /** Uin8Array with zero items */
4
6
  var EMPTY_UINT8_ARRAY = new Uint8Array(0);
5
7
 
6
8
  /**
7
- * Generates the Name-Based UUID hashes v3 and v5 according to RFC-4122
8
- * https://tools.ietf.org/html/rfc4122#section-4.3
9
+ * Generates the Name-Based UUID v3 and v5.
10
+ *
11
+ * With a namespace, output is compliant with RFC 9562 (formerly RFC 4122).
12
+ * Without a namespace, only the name is hashed: this is not described by the
13
+ * RFC, but for v3 it matches Java's `UUID.nameUUIDFromBytes`.
14
+ * https://www.rfc-editor.org/rfc/rfc9562#section-5.3
9
15
  * @param {string} target Hashing target
10
- * @param {string} [namespace] Some name space within which generation occurs
11
- * @param {3|5} [version=5] Version of UUID. Available versions is 3 and 5
12
- * according to RFC-4122. The version is responsible for the hashing algorithm:
13
- * version 3 uses MD5, and version 5 uses SHA-1. Default is 5.
16
+ * @param {string} [namespace] Namespace UUID, e.g. `generateUuid.DNS`
17
+ * @param {3|5} [version=5] Version of UUID. Available versions are 3 and 5.
18
+ * The version is responsible for the hashing algorithm: version 3 uses MD5,
19
+ * and version 5 uses SHA-1. Default is 5.
14
20
  * @returns {string} UUID
15
21
  */
16
22
  function generateUuid(target, namespace, version) {
@@ -38,11 +44,21 @@ function generateUuid(target, namespace, version) {
38
44
  var buffer = lib.concatBuffers(namespaceCharBuffer, targetCharBuffer);
39
45
 
40
46
  // Getting hash
41
- var hash = version === 3 ? lib.md5Hash(buffer) : lib.sha1Hash(buffer);
47
+ var hash = version === 3 ? md5(buffer) : sha1(buffer);
42
48
 
43
49
  return lib.hashToUuid(hash, version);
44
50
  }
45
51
 
52
+ /**
53
+ * Standard namespace IDs defined by RFC 9562 (formerly RFC 4122)
54
+ * https://www.rfc-editor.org/rfc/rfc9562#section-6.6
55
+ */
56
+ generateUuid.NIL = '00000000-0000-0000-0000-000000000000';
57
+ generateUuid.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
58
+ generateUuid.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
59
+ generateUuid.OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
60
+ generateUuid.X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
61
+
46
62
  /**
47
63
  * Export module
48
64
  */
package/src/lib.js CHANGED
@@ -1,6 +1,3 @@
1
- var md5 = require('js-md5');
2
- var sha1 = require('js-sha1');
3
-
4
1
  /** List of hex digit for fast accessing by index */
5
2
  var HEX_DIGITS = '0123456789abcdef'.split('');
6
3
 
@@ -66,24 +63,6 @@ var stringToCharBuffer = function (str) {
66
63
  return buffer;
67
64
  };
68
65
 
69
- /**
70
- * Generates MD5 hash from buffer
71
- * @param {Uint8Array} buf Buffer of char codes
72
- * @returns {Uint8Array} MD5 hash buffer
73
- */
74
- var md5Hash = function (buf) {
75
- return new Uint8Array(md5.arrayBuffer(buf));
76
- };
77
-
78
- /**
79
- * Generates SHA-1 hash from buffer
80
- * @param {Uint8Array} buf Buffer of char codes
81
- * @returns {Uint8Array} SHA-1 hash buffer
82
- */
83
- var sha1Hash = function (buf) {
84
- return new Uint8Array(sha1.arrayBuffer(buf));
85
- };
86
-
87
66
  /**
88
67
  * Concatenates two uint8 buffers
89
68
  * @param {Uint8Array} buf1 The first buffer to concatenate
@@ -170,8 +149,6 @@ module.exports = {
170
149
  uint8ToHex: uint8ToHex,
171
150
  uint8ArrayToHex: uint8ArrayToHex,
172
151
  stringToCharBuffer: stringToCharBuffer,
173
- md5Hash: md5Hash,
174
- sha1Hash: sha1Hash,
175
152
  concatBuffers: concatBuffers,
176
153
  validateUuid: validateUuid,
177
154
  parseUuid: parseUuid,
package/src/md5.js ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * MD5 hash function according to RFC 1321
3
+ * https://www.rfc-editor.org/rfc/rfc1321
4
+ *
5
+ * A minimal implementation for the library's needs: hashes a whole byte
6
+ * buffer at once. Works in any environment, has no dependencies and does not
7
+ * use `eval` or Node.js modules.
8
+ */
9
+
10
+ /** Per-round shift amounts */
11
+ var SHIFTS = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
12
+
13
+ /**
14
+ * Constants T[1..64] from RFC 1321, section 3.4: floor(abs(sin(i)) * 2^32).
15
+ * Hard-coded because Math.sin precision is implementation-defined.
16
+ */
17
+ var K = new Int32Array([
18
+ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
19
+ 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
20
+ 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
21
+ 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
22
+ 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
23
+ 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
24
+ 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
25
+ 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
26
+ 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
27
+ 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
28
+ 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
29
+ 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
30
+ 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
31
+ 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
32
+ 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
33
+ 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
34
+ ]);
35
+
36
+ /**
37
+ * Generates MD5 hash
38
+ * @param {Uint8Array} bytes Message bytes
39
+ * @returns {Uint8Array} 16-byte hash
40
+ */
41
+ function md5(bytes) {
42
+ var length = bytes.length;
43
+
44
+ // Padding: the message, a single 0x80 byte, zeros and the 64-bit
45
+ // little-endian message length in bits, to a multiple of 64 bytes
46
+ var paddedLength = (((length + 8) >>> 6) + 1) << 6;
47
+ var message = new Uint8Array(paddedLength);
48
+ var bitLengthLow = (length << 3) >>> 0;
49
+ var bitLengthHigh = Math.floor(length / 0x20000000);
50
+ var i;
51
+
52
+ message.set(bytes);
53
+ message[length] = 0x80;
54
+
55
+ for (i = 0; i < 4; i++) {
56
+ message[paddedLength - 8 + i] = (bitLengthLow >>> (8 * i)) & 0xff;
57
+ message[paddedLength - 4 + i] = (bitLengthHigh >>> (8 * i)) & 0xff;
58
+ }
59
+
60
+ var a0 = 0x67452301;
61
+ var b0 = 0xefcdab89 | 0;
62
+ var c0 = 0x98badcfe | 0;
63
+ var d0 = 0x10325476;
64
+ var words = new Int32Array(16);
65
+
66
+ for (var offset = 0; offset < paddedLength; offset += 64) {
67
+ for (i = 0; i < 16; i++) {
68
+ var p = offset + i * 4;
69
+ words[i] = message[p] | (message[p + 1] << 8) | (message[p + 2] << 16) | (message[p + 3] << 24);
70
+ }
71
+
72
+ var a = a0;
73
+ var b = b0;
74
+ var c = c0;
75
+ var d = d0;
76
+
77
+ for (i = 0; i < 64; i++) {
78
+ var f;
79
+ var g;
80
+
81
+ if (i < 16) {
82
+ f = (b & c) | (~b & d);
83
+ g = i;
84
+ } else if (i < 32) {
85
+ f = (d & b) | (~d & c);
86
+ g = (5 * i + 1) & 15;
87
+ } else if (i < 48) {
88
+ f = b ^ c ^ d;
89
+ g = (3 * i + 5) & 15;
90
+ } else {
91
+ f = c ^ (b | ~d);
92
+ g = (7 * i) & 15;
93
+ }
94
+
95
+ var sum = (a + f + K[i] + words[g]) | 0;
96
+ var shift = SHIFTS[((i >> 4) << 2) | (i & 3)];
97
+
98
+ a = d;
99
+ d = c;
100
+ c = b;
101
+ b = (b + ((sum << shift) | (sum >>> (32 - shift)))) | 0;
102
+ }
103
+
104
+ a0 = (a0 + a) | 0;
105
+ b0 = (b0 + b) | 0;
106
+ c0 = (c0 + c) | 0;
107
+ d0 = (d0 + d) | 0;
108
+ }
109
+
110
+ // Output: a0, b0, c0, d0 as little-endian bytes
111
+ var state = [a0, b0, c0, d0];
112
+ var out = new Uint8Array(16);
113
+
114
+ for (i = 0; i < 16; i++) {
115
+ out[i] = (state[i >> 2] >>> (8 * (i & 3))) & 0xff;
116
+ }
117
+
118
+ return out;
119
+ }
120
+
121
+ module.exports = md5;
package/src/sha1.js ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ * SHA-1 hash function according to FIPS 180-4
3
+ * https://csrc.nist.gov/pubs/fips/180-4/upd1/final
4
+ *
5
+ * A minimal implementation for the library's needs: hashes a whole byte
6
+ * buffer at once. Works in any environment, has no dependencies and does not
7
+ * use `eval` or Node.js modules.
8
+ */
9
+
10
+ /**
11
+ * Generates SHA-1 hash
12
+ * @param {Uint8Array} bytes Message bytes
13
+ * @returns {Uint8Array} 20-byte hash
14
+ */
15
+ function sha1(bytes) {
16
+ var length = bytes.length;
17
+
18
+ // Padding: the message, a single 0x80 byte, zeros and the 64-bit
19
+ // big-endian message length in bits, to a multiple of 64 bytes
20
+ var paddedLength = (((length + 8) >>> 6) + 1) << 6;
21
+ var message = new Uint8Array(paddedLength);
22
+ var bitLengthLow = (length << 3) >>> 0;
23
+ var bitLengthHigh = Math.floor(length / 0x20000000);
24
+ var i;
25
+
26
+ message.set(bytes);
27
+ message[length] = 0x80;
28
+
29
+ for (i = 0; i < 4; i++) {
30
+ message[paddedLength - 1 - i] = (bitLengthLow >>> (8 * i)) & 0xff;
31
+ message[paddedLength - 5 - i] = (bitLengthHigh >>> (8 * i)) & 0xff;
32
+ }
33
+
34
+ var h0 = 0x67452301;
35
+ var h1 = 0xefcdab89 | 0;
36
+ var h2 = 0x98badcfe | 0;
37
+ var h3 = 0x10325476;
38
+ var h4 = 0xc3d2e1f0 | 0;
39
+ var words = new Int32Array(80);
40
+
41
+ for (var offset = 0; offset < paddedLength; offset += 64) {
42
+ for (i = 0; i < 16; i++) {
43
+ var p = offset + i * 4;
44
+ words[i] = (message[p] << 24) | (message[p + 1] << 16) | (message[p + 2] << 8) | message[p + 3];
45
+ }
46
+
47
+ for (i = 16; i < 80; i++) {
48
+ var w = words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16];
49
+ words[i] = (w << 1) | (w >>> 31);
50
+ }
51
+
52
+ var a = h0;
53
+ var b = h1;
54
+ var c = h2;
55
+ var d = h3;
56
+ var e = h4;
57
+
58
+ for (i = 0; i < 80; i++) {
59
+ var f;
60
+ var k;
61
+
62
+ if (i < 20) {
63
+ f = (b & c) | (~b & d);
64
+ k = 0x5a827999;
65
+ } else if (i < 40) {
66
+ f = b ^ c ^ d;
67
+ k = 0x6ed9eba1;
68
+ } else if (i < 60) {
69
+ f = (b & c) | (b & d) | (c & d);
70
+ k = 0x8f1bbcdc | 0;
71
+ } else {
72
+ f = b ^ c ^ d;
73
+ k = 0xca62c1d6 | 0;
74
+ }
75
+
76
+ var temp = (((a << 5) | (a >>> 27)) + f + e + k + words[i]) | 0;
77
+
78
+ e = d;
79
+ d = c;
80
+ c = (b << 30) | (b >>> 2);
81
+ b = a;
82
+ a = temp;
83
+ }
84
+
85
+ h0 = (h0 + a) | 0;
86
+ h1 = (h1 + b) | 0;
87
+ h2 = (h2 + c) | 0;
88
+ h3 = (h3 + d) | 0;
89
+ h4 = (h4 + e) | 0;
90
+ }
91
+
92
+ // Output: h0..h4 as big-endian bytes
93
+ var state = [h0, h1, h2, h3, h4];
94
+ var out = new Uint8Array(20);
95
+
96
+ for (i = 0; i < 20; i++) {
97
+ out[i] = (state[i >> 2] >>> (24 - 8 * (i & 3))) & 0xff;
98
+ }
99
+
100
+ return out;
101
+ }
102
+
103
+ module.exports = sha1;
package/.eslintrc DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "node": true
5
- },
6
- "parserOptions": {
7
- "ecmaVersion": 5
8
- },
9
- "ignorePatterns": "tests"
10
- }
@@ -1,25 +0,0 @@
1
- name: Node.js CI
2
-
3
- on:
4
- push:
5
- branches: ['master']
6
- pull_request:
7
- branches: ['master']
8
-
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
-
13
- strategy:
14
- matrix:
15
- node-version: [12.x, 14.x, 16.x]
16
-
17
- steps:
18
- - uses: actions/checkout@v3
19
- - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v3
21
- with:
22
- node-version: ${{ matrix.node-version }}
23
- cache: 'npm'
24
- - run: npm i
25
- - run: npm test