Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Generate Password Operation #1879

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
"Disassemble x86",
"Pseudo-Random Number Generator",
"Generate De Bruijn Sequence",
"Generate Password",
"Generate UUID",
"Generate TOTP",
"Generate HOTP",
Expand Down
73 changes: 73 additions & 0 deletions src/core/operations/GeneratePassword.mjs
Original file line number Diff line number Diff line change
@@ -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.<br><br>This tool is intended to create random passwords for various use cases; however, it does not guarantee absolute security or protection against advanced attacks.<br><br>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];

Check warning on line 53 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed

Check warning on line 53 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed
const baseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerAlphabet = baseAlphabet.toLowerCase();
const symbolSet = "!@#$%^&*()-=_+|'\"";
const numberSet = "123456789";

Check warning on line 58 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed

Check warning on line 58 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed
let alphabet = baseAlphabet + lowerAlphabet;
alphabet += symbols ? symbolSet : "";
alphabet += numbers ? numberSet : "";

Check warning on line 62 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed

Check warning on line 62 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed
const resultArray = new Array(len);
for (let i = 0; i < len; i++) {
resultArray[i] = alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}

Check warning on line 67 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed

Check warning on line 67 in src/core/operations/GeneratePassword.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed
return resultArray.join("");
}

}

export default GeneratePassword;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
57 changes: 57 additions & 0 deletions tests/operations/tests/GeneratePassword.mjs
Original file line number Diff line number Diff line change
@@ -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]
},
],
}
]);
Loading