-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPassGenerator.java
More file actions
45 lines (38 loc) · 1.9 KB
/
Copy pathRandomPassGenerator.java
File metadata and controls
45 lines (38 loc) · 1.9 KB
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
import java.security.SecureRandom;
import java.util.Random;
public class RandomPassGenerator {
private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String DIGITS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_+=<>?";
public static String generateRandomPassword(int length, boolean useLowercase, boolean useUppercase, boolean useDigits, boolean useSpecialChars) {
// Step 3: Check Character Set Selection
if (!(useLowercase || useUppercase || useDigits || useSpecialChars)) {
throw new IllegalArgumentException("At least one character set must be selected.");
}
// Step 4: Generate the Password
StringBuilder password = new StringBuilder();
Random random = new SecureRandom();
StringBuilder characters = new StringBuilder();
if (useLowercase) characters.append(LOWERCASE);
if (useUppercase) characters.append(UPPERCASE);
if (useDigits) characters.append(DIGITS);
if (useSpecialChars) characters.append(SPECIAL_CHARACTERS);
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(characters.length());
password.append(characters.charAt(randomIndex));
}
// Step 5: Return the Password
return password.toString();
}
public static void main(String[] args) {
// Step 6: Example Usage
int length = 12;
boolean useLowercase = true;
boolean useUppercase = true;
boolean useDigits = true;
boolean useSpecialChars = true;
String randomPassword = generateRandomPassword(length, useLowercase, useUppercase, useDigits, useSpecialChars);
System.out.println("Generated Password: " + randomPassword);
}
}