From 27181b12eb667790067f7846301671a7de973843 Mon Sep 17 00:00:00 2001 From: Ifhezu <90856703+Ifhezu@users.noreply.github.com> Date: Sat, 14 Oct 2023 09:30:08 +0600 Subject: [PATCH 1/2] Create PasswordGenerator.java Just the basic logic outline. Works flawlessly as I tested it. To check, create the PasswordGenerator.class file and you should be good to go. Make sure you space out a bit for adding this to the main project. Remember, people cant get access to the task manager unless they create an account, and to create an account, they need a password, hence this generator comes into play. Just basic and simple logic of if statements tbh. Heres the method on how to run it: (In your terminal window) 1) Javac PasswordGenerator.java 2) Java PasswordGenerator 3) Follow along and type whatever you are prompted with. Tell me if you find any errors or anything for improvement, obviously its still in development. Signed-off-by: Ifhezu <90856703+Ifhezu@users.noreply.github.com> --- PasswordGenerator.java | 137 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 PasswordGenerator.java diff --git a/PasswordGenerator.java b/PasswordGenerator.java new file mode 100644 index 0000000..db1103c --- /dev/null +++ b/PasswordGenerator.java @@ -0,0 +1,137 @@ +import java.util.*; + +public class PasswordGenerator { + private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; + private static final String NUMBERS = "0123456789"; + private static final String SYMBOLS = "!@#$%^&*()_-+=<>?"; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // User Input and Preferences + int passwordLength = getPasswordLength(scanner); + String characterTypes = getCharacterTypes(scanner); + + // Generating a Random Password + String generatedPassword = generatePassword(passwordLength, characterTypes); + + // Password Strength + String passwordStrength = getPasswordStrength(generatedPassword); + + // Display or Copy Password + System.out.println("Generated Password: " + generatedPassword); + System.out.println("Password Strength: " + passwordStrength); + + scanner.close(); + } + + private static int getPasswordLength(Scanner scanner) { + int length = 0; + boolean validInput = false; + + while (!validInput) { + System.out.print("Enter the desired password length: "); + String input = scanner.nextLine(); + + try { + length = Integer.parseInt(input); + if (length >= 8) { + validInput = true; + } else { + System.out.println("Password length should be at least 8 characters."); + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid integer."); + } + } + + return length; + } + + private static String getCharacterTypes(Scanner scanner) { + System.out.println("Select the character types to include in the password:"); + System.out.println("1. Uppercase letters"); + System.out.println("2. Lowercase letters"); + System.out.println("3. Numbers"); + System.out.println("4. Symbols"); + System.out.println("Enter the corresponding numbers separated by commas (e.g., 1,3): "); + + String input = scanner.nextLine(); + + Set characterSet = new HashSet<>(); + String[] choices = input.split(","); + + for (String choice : choices) { + switch (choice.trim()) { + case "1": + addCharactersToSet(characterSet, UPPERCASE_LETTERS); + break; + case "2": + addCharactersToSet(characterSet, LOWERCASE_LETTERS); + break; + case "3": + addCharactersToSet(characterSet, NUMBERS); + break; + case "4": + addCharactersToSet(characterSet, SYMBOLS); + break; + default: + System.out.println("Invalid choice: " + choice); + break; + } + } + + StringBuilder characterTypes = new StringBuilder(); + for (char c : characterSet) { + characterTypes.append(c); + } + + return characterTypes.toString(); + } + + private static void addCharactersToSet(Set characterSet, String characters) { + for (char c : characters.toCharArray()) { + characterSet.add(c); + } + } + + private static String generatePassword(int length, String characterTypes) { + StringBuilder password = new StringBuilder(); + + Random random = new Random(); + for (int i = 0; i < length; i++) { + int randomIndex = random.nextInt(characterTypes.length()); + password.append(characterTypes.charAt(randomIndex)); + } + + return password.toString(); + } + + private static String getPasswordStrength(String password) { + boolean hasUppercase = false; + boolean hasLowercase = false; + boolean hasNumber = false; + boolean hasSymbol = false; + + for (char c : password.toCharArray()) { + if (Character.isUpperCase(c)) { + hasUppercase = true; + } else if (Character.isLowerCase(c)) { + hasLowercase = true; + } else if (Character.isDigit(c)) { + hasNumber = true; + } else { + hasSymbol = true; + } + } + + if (password.length() >= 12 && hasUppercase && hasLowercase && hasNumber && hasSymbol) { + return "Strong"; + } else if (password.length() >= 8 && (hasUppercase || hasLowercase || hasNumber || hasSymbol)) { + return "Medium"; + } else { + return "Weak"; + } + } +} From 061ddf164096b10c0e062a4f4e88be115846a3dd Mon Sep 17 00:00:00 2001 From: Ifhezu <90856703+Ifhezu@users.noreply.github.com> Date: Sat, 14 Oct 2023 10:04:35 +0600 Subject: [PATCH 2/2] Update PasswordGenerator.java Signed-off-by: Ifhezu <90856703+Ifhezu@users.noreply.github.com> --- PasswordGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PasswordGenerator.java b/PasswordGenerator.java index db1103c..ea27dbe 100644 --- a/PasswordGenerator.java +++ b/PasswordGenerator.java @@ -55,7 +55,7 @@ private static String getCharacterTypes(Scanner scanner) { System.out.println("2. Lowercase letters"); System.out.println("3. Numbers"); System.out.println("4. Symbols"); - System.out.println("Enter the corresponding numbers separated by commas (e.g., 1,3): "); + System.out.print("Enter the corresponding numbers separated by commas (e.g., 1,3): "); // Edit: 10/14/2023 10:04 AM (Changed println to print) String input = scanner.nextLine();