This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
PasswordGenerator.java
66 lines (51 loc) · 2.44 KB
/
PasswordGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.security.SecureRandom;
import java.util.Scanner;
public class PasswordGenerator {
private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String DIGITS = "0123456789";
private static final String SYMBOLS = "!@#$%^&*()-_=+[]{}|;:',.<>?/";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SecureRandom random = new SecureRandom();
System.out.print("Enter the desired length of the password: ");
int length = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.print("Include uppercase letters? (y/n): ");
boolean includeUppercase = scanner.nextLine().equalsIgnoreCase("y");
System.out.print("Include lowercase letters? (y/n): ");
boolean includeLowercase = scanner.nextLine().equalsIgnoreCase("y");
System.out.print("Include digits? (y/n): ");
boolean includeDigits = scanner.nextLine().equalsIgnoreCase("y");
System.out.print("Include symbols? (y/n): ");
boolean includeSymbols = scanner.nextLine().equalsIgnoreCase("y");
String password = generatePassword(length, includeUppercase, includeLowercase, includeDigits, includeSymbols, random);
System.out.println("Generated Password: " + password);
scanner.close();
}
private static String generatePassword(int length, boolean includeUppercase, boolean includeLowercase,
boolean includeDigits, boolean includeSymbols, SecureRandom random) {
StringBuilder characterSet = new StringBuilder();
if (includeLowercase) {
characterSet.append(LOWERCASE);
}
if (includeUppercase) {
characterSet.append(UPPERCASE);
}
if (includeDigits) {
characterSet.append(DIGITS);
}
if (includeSymbols) {
characterSet.append(SYMBOLS);
}
if (characterSet.length() == 0) {
throw new IllegalArgumentException("At least one character set must be selected.");
}
StringBuilder password = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = random.nextInt(characterSet.length());
password.append(characterSet.charAt(index));
}
return password.toString();
}
}