From 54d6c7b506570d0be151b5598bfeb2afd51b0244 Mon Sep 17 00:00:00 2001 From: Niall Date: Wed, 21 Aug 2024 22:16:23 +0000 Subject: [PATCH 1/3] Generates a password of given length and complexity based on optional inputs for the inclusion of symbols and numbers. --- src/core/config/Categories.json | 1 + src/core/operations/GeneratePassword.mjs | 73 +++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/GeneratePassword.mjs | 57 ++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 src/core/operations/GeneratePassword.mjs create mode 100644 tests/operations/tests/GeneratePassword.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index bebdd6a5e2..ad6f5023bc 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -528,6 +528,7 @@ "Disassemble x86", "Pseudo-Random Number Generator", "Generate De Bruijn Sequence", + "Generate Password", "Generate UUID", "Generate TOTP", "Generate HOTP", diff --git a/src/core/operations/GeneratePassword.mjs b/src/core/operations/GeneratePassword.mjs new file mode 100644 index 0000000000..6e9900d744 --- /dev/null +++ b/src/core/operations/GeneratePassword.mjs @@ -0,0 +1,73 @@ +/** + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Generate Password operation + */ +class GeneratePassword extends Operation { + + /** + * GenPassword constructor + */ + constructor() { + super(); + + this.name = "Generate Password"; + this.module = "Default"; + this.description = "Generate a random password based on specified length, and optional inclusion of symbols and numbers.

This tool is intended to create random passwords for various use cases; however, it does not guarantee absolute security or protection against advanced attacks.

Users are advised to combine this tool with best practices in password management, including the use of password managers, and to follow security guidelines to ensure the strength and safety of their passwords."; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Length", + "type": "number", + "value": "24", + }, + { + "name": "Symbols", + "type": "boolean", + "value": false + }, + { + "name": "Numbers", + "type": "boolean", + "value": false + }, + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + async run(input, args) { + const len = args[0]; + const symbols = args[1]; + const numbers = args[2]; + + const baseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const lowerAlphabet = baseAlphabet.toLowerCase(); + const symbolSet = "!@#$%^&*()-=_+|'\""; + const numberSet = "123456789"; + + let alphabet = baseAlphabet + lowerAlphabet; + alphabet += symbols ? symbolSet : ""; + alphabet += numbers ? numberSet : ""; + + const resultArray = new Array(len); + for (let i = 0; i < len; i++) { + resultArray[i] = alphabet.charAt(Math.floor(Math.random() * alphabet.length)); + } + + return resultArray.join(''); + } + +} + +export default GeneratePassword; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 40ce7a2ee6..708fb7066b 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -71,6 +71,7 @@ import "./tests/Fork.mjs"; import "./tests/FromDecimal.mjs"; import "./tests/GenerateAllHashes.mjs"; import "./tests/GenerateDeBruijnSequence.mjs"; +import "./tests/GeneratePassword.mjs"; import "./tests/GetAllCasings.mjs"; import "./tests/GOST.mjs"; import "./tests/Gunzip.mjs"; diff --git a/tests/operations/tests/GeneratePassword.mjs b/tests/operations/tests/GeneratePassword.mjs new file mode 100644 index 0000000000..07ef4c73b7 --- /dev/null +++ b/tests/operations/tests/GeneratePassword.mjs @@ -0,0 +1,57 @@ +/** + * GeneratePassword tests. + * + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + + +TestRegister.addTests([ + { + name: "Generate random string of length 10 without symbols or numbers", + input: "", + expectedMatch: /^[A-Za-z]{10}$/, + recipeConfig: [ + { + "op": "Generate Password", + "args": [10, false, false] + }, + ], + }, + { + name: "Generate random string of length 15 with symbols but without numbers", + input: "", + expectedMatch: /^[A-Za-z!@#$%^&*()\-=+_|\\"']{15}$/, + recipeConfig: [ + { + "op": "Generate Password", + "args": [15, true, false] + }, + ], + }, + { + name: "Generate random string of length 20 with numbers but without symbols", + input: "", + expectedMatch: /^[A-Za-z0-9]{20}$/, + recipeConfig: [ + { + "op": "Generate Password", + "args": [20, false, true] + }, + ], + }, + { + name: "Generate random string of length 25 with both symbols and numbers", + input: "", + expectedMatch: /^[A-Za-z0-9!@#$%^&*()\-=+_|\\"']{25}$/, + recipeConfig: [ + { + "op": "Generate Password", + "args": [25, true, true] + }, + ], + } +]); \ No newline at end of file From 068a94d61b256f4788c5fb25123befcff9bef70e Mon Sep 17 00:00:00 2001 From: Niall Date: Wed, 21 Aug 2024 22:20:57 +0000 Subject: [PATCH 2/3] Patch for testing error > Strings must use doublequote quotes --- src/core/operations/GeneratePassword.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/GeneratePassword.mjs b/src/core/operations/GeneratePassword.mjs index 6e9900d744..0ad034b5b6 100644 --- a/src/core/operations/GeneratePassword.mjs +++ b/src/core/operations/GeneratePassword.mjs @@ -65,7 +65,7 @@ class GeneratePassword extends Operation { resultArray[i] = alphabet.charAt(Math.floor(Math.random() * alphabet.length)); } - return resultArray.join(''); + return resultArray.join(""); } } From e5eb84682b4b9b30b678bbeb96779adce96437b5 Mon Sep 17 00:00:00 2001 From: Niall Date: Wed, 21 Aug 2024 22:23:08 +0000 Subject: [PATCH 3/3] Patch for testing error > Newline required at end of file but not found eol-last --- tests/operations/tests/GeneratePassword.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/operations/tests/GeneratePassword.mjs b/tests/operations/tests/GeneratePassword.mjs index 07ef4c73b7..ecc764a1f4 100644 --- a/tests/operations/tests/GeneratePassword.mjs +++ b/tests/operations/tests/GeneratePassword.mjs @@ -54,4 +54,4 @@ TestRegister.addTests([ }, ], } -]); \ No newline at end of file +]);