diff --git a/package-lock.json b/package-lock.json index ef2da3f0..53aa7ba6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -94,6 +94,7 @@ "ua-parser-js": "^1.0.38", "unorm": "^1.6.0", "utf8": "^3.0.0", + "uuid": "^11.1.0", "vkbeautify": "^0.99.3", "xpath": "0.0.34", "xregexp": "^5.1.1", @@ -13876,6 +13877,15 @@ "node": ">=8" } }, + "node_modules/nightwatch/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/nightwatch/node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", @@ -16819,6 +16829,15 @@ "node": ">=0.8.0" } }, + "node_modules/sockjs/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/socks": { "version": "2.8.3", "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.3.tgz", @@ -18106,13 +18125,15 @@ } }, "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "dev": true, - "license": "MIT", + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.0.tgz", + "integrity": "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], "bin": { - "uuid": "dist/bin/uuid" + "uuid": "dist/esm/bin/uuid" } }, "node_modules/v8flags": { diff --git a/package.json b/package.json index b3492a8e..847ace80 100644 --- a/package.json +++ b/package.json @@ -180,6 +180,7 @@ "ua-parser-js": "^1.0.38", "unorm": "^1.6.0", "utf8": "^3.0.0", + "uuid": "^11.1.0", "vkbeautify": "^0.99.3", "xpath": "0.0.34", "xregexp": "^5.1.1", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 71b311e6..0b3aa90a 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -545,6 +545,7 @@ "Pseudo-Random Number Generator", "Generate De Bruijn Sequence", "Generate UUID", + "Analyse UUID", "Generate TOTP", "Generate HOTP", "Generate QR Code", diff --git a/src/core/operations/AnalyseUUID.mjs b/src/core/operations/AnalyseUUID.mjs new file mode 100644 index 00000000..b3506017 --- /dev/null +++ b/src/core/operations/AnalyseUUID.mjs @@ -0,0 +1,48 @@ +/** + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2023 + * @license Apache-2.0 + */ + +import * as uuid from "uuid"; + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Analyse UUID operation + */ +class AnalyseUUID extends Operation { + + /** + * AnalyseUUID constructor + */ + constructor() { + super(); + + this.name = "Analyse UUID"; + this.module = "Crypto"; + this.description = "Tries to determine information about a given UUID and suggests which version may have been used to generate it"; + this.infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier"; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + try { + const uuidVersion = uuid.version(input); + return "UUID version: " + uuidVersion; + } catch (error) { + throw new OperationError("Invalid UUID"); + } + } + +} + +export default AnalyseUUID; diff --git a/src/core/operations/GenerateUUID.mjs b/src/core/operations/GenerateUUID.mjs index 1ee0faba..21d063e3 100644 --- a/src/core/operations/GenerateUUID.mjs +++ b/src/core/operations/GenerateUUID.mjs @@ -5,8 +5,8 @@ */ import Operation from "../Operation.mjs"; -import crypto from "crypto"; - +import * as uuid from "uuid"; +import OperationError from "../errors/OperationError.mjs"; /** * Generate UUID operation */ @@ -20,11 +20,38 @@ class GenerateUUID extends Operation { this.name = "Generate UUID"; this.module = "Crypto"; - this.description = "Generates an RFC 4122 version 4 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).

A version 4 UUID relies on random numbers, in this case generated using window.crypto if available and falling back to Math.random if not."; + this.description = + "Generates an RFC 9562 (formerly RFC 4122) compliant Universally Unique Identifier (UUID), " + + "also known as a Globally Unique Identifier (GUID).
" + + "
" + + "We currently support generating the following UUID versions:
" + + "" + + "UUIDs are generated using the uuid package.
"; this.infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier"; this.inputType = "string"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Version", + hint: "UUID version", + type: "option", + value: ["v1", "v3", "v4", "v5", "v6", "v7"], + defaultIndex: 2, + }, + { + name: "Namespace", + hint: "UUID namespace (UUID; valid for v3 and v5)", + type: "string", + value: "1b671a64-40d5-491e-99b0-da01ff1f3341" + } + ]; } /** @@ -33,16 +60,17 @@ class GenerateUUID extends Operation { * @returns {string} */ run(input, args) { - const buf = new Uint32Array(4).map(() => { - return crypto.randomBytes(4).readUInt32BE(0, true); - }); - let i = 0; - return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) { - const r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf, - v = c === "x" ? r : (r & 0x3 | 0x8); - i++; - return v.toString(16); - }); + const [version, namespace] = args; + const hasDesiredVersion = typeof uuid[version] === "function"; + if (!hasDesiredVersion) throw new OperationError("Invalid UUID version"); + + const requiresNamespace = ["v3", "v5"].includes(version); + if (!requiresNamespace) return uuid[version](); + + const hasValidNamespace = typeof namespace === "string" && uuid.validate(namespace); + if (!hasValidNamespace) throw new OperationError("Invalid UUID namespace"); + + return uuid[version](input, namespace); } } diff --git a/tests/node/tests/operations.mjs b/tests/node/tests/operations.mjs index 4c5d4ada..022b0701 100644 --- a/tests/node/tests/operations.mjs +++ b/tests/node/tests/operations.mjs @@ -580,10 +580,25 @@ Password: 282760`; assert.strictEqual(result.toString().substr(0, 37), "-----BEGIN PGP PRIVATE KEY BLOCK-----"); }), - it("Generate UUID", () => { - const result = chef.generateUUID(); - assert.ok(result.toString()); - assert.strictEqual(result.toString().length, 36); + ...[1, 3, 4, 5, 6, 7].map(version => it(`Generate UUID v${version}`, () => { + const result = chef.generateUUID("", { "version": `v${version}` }).toString(); + assert.ok(result); + assert.strictEqual(result.length, 36); + })), + + ...[1, 3, 4, 5, 6, 7].map(version => it(`Analyze UUID v${version}`, () => { + const uuid = chef.generateUUID("", { "version": `v${version}` }).toString(); + const result = chef.analyseUUID(uuid).toString(); + const expected = `UUID version: ${version}`; + assert.strictEqual(result, expected); + })), + + it("Generate UUID using defaults", () => { + const uuid = chef.generateUUID(); + assert.ok(uuid); + + const analysis = chef.analyseUUID(uuid).toString(); + assert.strictEqual(analysis, "UUID version: 4"); }), it("Gzip, Gunzip", () => {