-
Notifications
You must be signed in to change notification settings - Fork 0
Create PasswordGenerator.java #5
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
Open
Ifhezu
wants to merge
2
commits into
main
Choose a base branch
from
Ifhezu-patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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(); | ||
|
||
Set<Character> 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<Character> 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"; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.