-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordGenerator.java
54 lines (44 loc) · 2.02 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
package RandomPasswordGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
public class PasswordGenerator {
private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
private static final String DIGITS = "0123456789";
private static final String SPECIAL = "!@#$%^&*()-_=+[]{}|;:'\",.<>?";
public static String generatePassword(int length, boolean useUpper, boolean useLower, boolean useDigits, boolean useSpecial) {
if (length <= 0) {
throw new IllegalArgumentException("Password length must be greater than 0");
}
StringBuilder characters = new StringBuilder();
if (useUpper) characters.append(UPPER);
if (useLower) characters.append(LOWER);
if (useDigits) characters.append(DIGITS);
if (useSpecial) characters.append(SPECIAL);
if (characters.length() == 0) {
throw new IllegalArgumentException("At least one character type (upper, lower, digits, special) must be selected");
}
Random random;
try {
random = SecureRandom.getInstanceStrong(); // Strong cryptographic random number generator
} catch (NoSuchAlgorithmException e) {
random = new SecureRandom();
}
StringBuilder password = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(characters.length());
password.append(characters.charAt(randomIndex));
}
return password.toString();
}
public static void main(String[] args) {
int length = 12; // Change the desired password length
boolean useUpper = true;
boolean useLower = true;
boolean useDigits = true;
boolean useSpecial = true;
String password = generatePassword(length, useUpper, useLower, useDigits, useSpecial);
System.out.println("Generated Password: " + password);
}
}