-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
32 lines (26 loc) · 1.04 KB
/
Copy pathPasswordGenerator.java
File metadata and controls
32 lines (26 loc) · 1.04 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
import java.util.Random;
public class PasswordGenerator {
private static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBERS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_+=<>?";
public static String generate(PasswordConfig config) {
StringBuilder password = new StringBuilder(config.getLength());
String characterPool = LOWER_CASE;
if (config.isUseUpperCase()) {
characterPool += UPPER_CASE;
}
if (config.isUseNumber()) {
characterPool += NUMBERS;
}
if (config.isUseSpecialCaracter()) {
characterPool += SPECIAL_CHARACTERS;
}
Random random = new Random();
for (int i = 0; i < config.getLength(); i++) {
int index = random.nextInt(characterPool.length());
password.append(characterPool.charAt(index));
}
return password.toString();
}
}