uuid-by-string 3.0.7 → 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/LICENSE +1 -1
- package/README.md +31 -5
- package/index.d.ts +23 -8
- package/package.json +12 -8
- package/src/index.js +26 -12
- package/src/lib.js +89 -55
- package/src/md5.js +121 -0
- package/src/sha1.js +103 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
uuid-by-string
|
|
2
2
|
[](https://www.npmjs.com/package/uuid-by-string)
|
|
3
3
|
[](https://www.npmjs.com/package/uuid-by-string)
|
|
4
|
-
[](https://travis-ci.org/danakt/uuid-by-string)
|
|
5
4
|
=======================
|
|
6
|
-
Generates
|
|
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
|
|
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_ — 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
|
|
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]
|
|
6
|
-
* @param {3|5} [version=
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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
|
-
|
|
15
|
-
|
|
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,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uuid-by-string",
|
|
3
|
-
"version": "
|
|
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
|
-
"
|
|
13
|
+
"lint": "eslint src",
|
|
14
|
+
"prepublishOnly": "npm run lint && npm run test"
|
|
10
15
|
},
|
|
11
16
|
"repository": {
|
|
12
17
|
"type": "git",
|
|
@@ -23,7 +28,7 @@
|
|
|
23
28
|
"md5",
|
|
24
29
|
"sha-1"
|
|
25
30
|
],
|
|
26
|
-
"author": "Danakt
|
|
31
|
+
"author": "Danakt Saushkin",
|
|
27
32
|
"license": "MIT",
|
|
28
33
|
"bugs": {
|
|
29
34
|
"url": "https://github.com/Danakt/uuid-by-string/issues"
|
|
@@ -38,11 +43,10 @@
|
|
|
38
43
|
"src/**"
|
|
39
44
|
]
|
|
40
45
|
},
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"js-md5": "^0.7.3",
|
|
43
|
-
"js-sha1": "^0.6.0"
|
|
44
|
-
},
|
|
45
46
|
"devDependencies": {
|
|
46
|
-
"
|
|
47
|
+
"@eslint/js": "^10.0.1",
|
|
48
|
+
"eslint": "^10.16.0",
|
|
49
|
+
"globals": "^17.15.0",
|
|
50
|
+
"jest": "^30.5.2"
|
|
47
51
|
}
|
|
48
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
|
|
8
|
-
*
|
|
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]
|
|
11
|
-
* @param {3|5} [version=5] Version of UUID. Available versions
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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) {
|
|
@@ -31,20 +37,28 @@ function generateUuid(target, namespace, version) {
|
|
|
31
37
|
}
|
|
32
38
|
|
|
33
39
|
// Parsing target chars
|
|
34
|
-
var
|
|
35
|
-
|
|
36
|
-
// TODO: Test namespace for uuid and parse to buffer
|
|
37
|
-
var namespaceCharBuffer = typeof namespace === 'string' ? lib.stringToCharBuffer(namespace) : EMPTY_UINT8_ARRAY;
|
|
40
|
+
var targetCharBuffer = lib.stringToCharBuffer(target);
|
|
41
|
+
var namespaceCharBuffer = typeof namespace === 'string' ? lib.parseUuid(namespace) : EMPTY_UINT8_ARRAY;
|
|
38
42
|
|
|
39
43
|
// Concatenation two buffers of strings to one
|
|
40
|
-
var buffer = lib.concatBuffers(namespaceCharBuffer,
|
|
44
|
+
var buffer = lib.concatBuffers(namespaceCharBuffer, targetCharBuffer);
|
|
41
45
|
|
|
42
46
|
// Getting hash
|
|
43
|
-
var hash = version === 3 ?
|
|
47
|
+
var hash = version === 3 ? md5(buffer) : sha1(buffer);
|
|
44
48
|
|
|
45
49
|
return lib.hashToUuid(hash, version);
|
|
46
50
|
}
|
|
47
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
|
+
|
|
48
62
|
/**
|
|
49
63
|
* Export module
|
|
50
64
|
*/
|
package/src/lib.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
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
|
|
|
4
|
+
/** Length of string containing uuid */
|
|
5
|
+
var UUID_LENGTH = 36;
|
|
6
|
+
|
|
7
|
+
/** Regular expression for uuid testing */
|
|
8
|
+
var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
9
|
+
|
|
10
|
+
/** Map matching of hex number and corresponding byte */
|
|
11
|
+
var HEX_TO_BYTE_MAP = (function () {
|
|
12
|
+
var map = {};
|
|
13
|
+
|
|
14
|
+
for (var i = 0; i < 256; i++) {
|
|
15
|
+
var hex = i.toString(16);
|
|
16
|
+
|
|
17
|
+
map[hex.length === 1 ? '0' + hex : hex] = i;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return map;
|
|
21
|
+
})();
|
|
22
|
+
|
|
7
23
|
/**
|
|
8
24
|
* Converts unsigned byte to hex representation
|
|
9
25
|
* @param {number} ubyte The unsigned byte to convert
|
|
10
26
|
* @returns {string} The hex representation
|
|
11
27
|
*/
|
|
12
|
-
var uint8ToHex = function(ubyte) {
|
|
28
|
+
var uint8ToHex = function (ubyte) {
|
|
13
29
|
var first = ubyte >> 4;
|
|
14
30
|
var second = ubyte - (first << 4);
|
|
15
31
|
|
|
@@ -21,14 +37,14 @@ var uint8ToHex = function(ubyte) {
|
|
|
21
37
|
* @param {Uint8Array} buf The unsigned bytes buffer
|
|
22
38
|
* @returns {string} The hex string representation
|
|
23
39
|
*/
|
|
24
|
-
var uint8ArrayToHex = function(buf) {
|
|
25
|
-
var out =
|
|
40
|
+
var uint8ArrayToHex = function (buf) {
|
|
41
|
+
var out = '';
|
|
26
42
|
|
|
27
43
|
for (var i = 0; i < buf.length; i++) {
|
|
28
|
-
out
|
|
44
|
+
out += uint8ToHex(buf[i]);
|
|
29
45
|
}
|
|
30
46
|
|
|
31
|
-
return out
|
|
47
|
+
return out;
|
|
32
48
|
};
|
|
33
49
|
|
|
34
50
|
/**
|
|
@@ -36,41 +52,24 @@ var uint8ArrayToHex = function(buf) {
|
|
|
36
52
|
* @param {string} str The string to parse
|
|
37
53
|
* @returns {Uint8Array} Buffer of char codes
|
|
38
54
|
*/
|
|
39
|
-
var stringToCharBuffer = function(str) {
|
|
40
|
-
var
|
|
55
|
+
var stringToCharBuffer = function (str) {
|
|
56
|
+
var escapedStr = unescape(encodeURIComponent(str));
|
|
57
|
+
var buffer = new Uint8Array(escapedStr.length);
|
|
41
58
|
|
|
42
|
-
for (var i = 0; i <
|
|
43
|
-
buffer[i] =
|
|
59
|
+
for (var i = 0; i < escapedStr.length; i++) {
|
|
60
|
+
buffer[i] = escapedStr[i].charCodeAt(0);
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
return buffer;
|
|
47
64
|
};
|
|
48
65
|
|
|
49
|
-
/**
|
|
50
|
-
* Generates MD5 hash from buffer
|
|
51
|
-
* @param {Uint8Array} buf Buffer of char codes
|
|
52
|
-
* @returns {Uint8Array} MD5 hash buffer
|
|
53
|
-
*/
|
|
54
|
-
var md5Hash = function(buf) {
|
|
55
|
-
return new Uint8Array(md5.arrayBuffer(buf));
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Generates SHA-1 hash from buffer
|
|
60
|
-
* @param {Uint8Array} buf Buffer of char codes
|
|
61
|
-
* @returns {Uint8Array} SHA-1 hash buffer
|
|
62
|
-
*/
|
|
63
|
-
var sha1Hash = function(buf) {
|
|
64
|
-
return new Uint8Array(sha1.arrayBuffer(buf));
|
|
65
|
-
};
|
|
66
|
-
|
|
67
66
|
/**
|
|
68
67
|
* Concatenates two uint8 buffers
|
|
69
68
|
* @param {Uint8Array} buf1 The first buffer to concatenate
|
|
70
69
|
* @param {Uint8Array} buf2 The second buffer to concatenate
|
|
71
70
|
* @returns {Uint8Array} Concatenation result
|
|
72
71
|
*/
|
|
73
|
-
var concatBuffers = function(buf1, buf2) {
|
|
72
|
+
var concatBuffers = function (buf1, buf2) {
|
|
74
73
|
var out = new Uint8Array(buf1.length + buf2.length);
|
|
75
74
|
|
|
76
75
|
out.set(new Uint8Array(buf1), 0);
|
|
@@ -79,44 +78,79 @@ var concatBuffers = function(buf1, buf2) {
|
|
|
79
78
|
return out;
|
|
80
79
|
};
|
|
81
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Validates UUID
|
|
83
|
+
* @param {string} uuid UUID to validate
|
|
84
|
+
* @return {boolean} Validation result
|
|
85
|
+
*/
|
|
86
|
+
var validateUuid = function (uuid) {
|
|
87
|
+
return typeof uuid === 'string' && uuid.length === UUID_LENGTH && UUID_REGEXP.test(uuid);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Parses UUID into a buffer
|
|
92
|
+
* @param {string} uuid UUID to parse
|
|
93
|
+
* @returns {Uint8Array} Ready buffer
|
|
94
|
+
*/
|
|
95
|
+
var parseUuid = function (uuid) {
|
|
96
|
+
if (!validateUuid(uuid)) {
|
|
97
|
+
throw TypeError('Invalid UUID');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
var buf = new Uint8Array(16);
|
|
101
|
+
var strIndex = 0;
|
|
102
|
+
var bufIndex = 0;
|
|
103
|
+
|
|
104
|
+
while (strIndex < uuid.length) {
|
|
105
|
+
if (uuid[strIndex] === '-') {
|
|
106
|
+
strIndex++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var oct = (uuid[strIndex] + uuid[strIndex + 1]).toLowerCase();
|
|
111
|
+
buf[bufIndex] = HEX_TO_BYTE_MAP[oct];
|
|
112
|
+
|
|
113
|
+
bufIndex++;
|
|
114
|
+
strIndex += 2;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return buf;
|
|
118
|
+
};
|
|
119
|
+
|
|
82
120
|
/**
|
|
83
121
|
* Creates uuid from hash buffer
|
|
84
122
|
* @param {Uint8Array} hashBuffer Hash buffer
|
|
85
123
|
* @param {3|5} version Version of uuid
|
|
86
124
|
* @returns {string} The uuid
|
|
87
125
|
*/
|
|
88
|
-
var hashToUuid = function(hashBuffer, version) {
|
|
89
|
-
return
|
|
126
|
+
var hashToUuid = function (hashBuffer, version) {
|
|
127
|
+
return (
|
|
90
128
|
// The low field of the timestamp
|
|
91
|
-
uint8ArrayToHex(hashBuffer.slice(0, 4))
|
|
92
|
-
'-'
|
|
93
|
-
|
|
129
|
+
uint8ArrayToHex(hashBuffer.slice(0, 4)) +
|
|
130
|
+
'-' +
|
|
94
131
|
// The middle field of the timestamp
|
|
95
|
-
uint8ArrayToHex(hashBuffer.slice(4, 6))
|
|
96
|
-
'-'
|
|
97
|
-
|
|
132
|
+
uint8ArrayToHex(hashBuffer.slice(4, 6)) +
|
|
133
|
+
'-' +
|
|
98
134
|
// The high field of the timestamp multiplexed with the version number
|
|
99
|
-
uint8ToHex((hashBuffer[6] & 0x0f) | parseInt(version * 10, 16))
|
|
100
|
-
uint8ToHex(hashBuffer[7])
|
|
101
|
-
'-'
|
|
102
|
-
|
|
135
|
+
uint8ToHex((hashBuffer[6] & 0x0f) | parseInt(version * 10, 16)) +
|
|
136
|
+
uint8ToHex(hashBuffer[7]) +
|
|
137
|
+
'-' +
|
|
103
138
|
// The high field of the clock sequence multiplexed with the variant
|
|
104
|
-
uint8ToHex((hashBuffer[8] & 0x3f) | 0x80)
|
|
139
|
+
uint8ToHex((hashBuffer[8] & 0x3f) | 0x80) +
|
|
105
140
|
// The low field of the clock sequence
|
|
106
|
-
uint8ToHex(hashBuffer[9])
|
|
107
|
-
'-'
|
|
141
|
+
uint8ToHex(hashBuffer[9]) +
|
|
142
|
+
'-' +
|
|
108
143
|
// The spatially unique node identifier
|
|
109
|
-
|
|
110
144
|
uint8ArrayToHex(hashBuffer.slice(10, 16))
|
|
111
|
-
|
|
145
|
+
);
|
|
112
146
|
};
|
|
113
147
|
|
|
114
148
|
module.exports = {
|
|
115
|
-
uint8ToHex,
|
|
116
|
-
uint8ArrayToHex,
|
|
117
|
-
stringToCharBuffer,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
hashToUuid
|
|
149
|
+
uint8ToHex: uint8ToHex,
|
|
150
|
+
uint8ArrayToHex: uint8ArrayToHex,
|
|
151
|
+
stringToCharBuffer: stringToCharBuffer,
|
|
152
|
+
concatBuffers: concatBuffers,
|
|
153
|
+
validateUuid: validateUuid,
|
|
154
|
+
parseUuid: parseUuid,
|
|
155
|
+
hashToUuid: hashToUuid,
|
|
122
156
|
};
|
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;
|