-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
46 lines (36 loc) · 2.13 KB
/
PasswordGenerator.java
File metadata and controls
46 lines (36 loc) · 2.13 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
46
import java.security.SecureRandom;
public class PasswordGenerator {
private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
private static final String DIGITS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()-_+=<>?";
private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS;
private static final SecureRandom random = new SecureRandom();
public static String generatePassword(int length) {
if (length < 1) throw new IllegalArgumentException("Password length must be greater than 0");
StringBuilder password = new StringBuilder(length);
// Ensure the password has at least one character from each character set
password.append(UPPERCASE_LETTERS.charAt(random.nextInt(UPPERCASE_LETTERS.length())));
password.append(LOWERCASE_LETTERS.charAt(random.nextInt(LOWERCASE_LETTERS.length())));
password.append(DIGITS.charAt(random.nextInt(DIGITS.length())));
password.append(SPECIAL_CHARACTERS.charAt(random.nextInt(SPECIAL_CHARACTERS.length())));
// Fill the remaining characters randomly from all characters
for (int i = 4; i < length; i++) {
password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length())));
}
// Shuffle the password to ensure randomness
char[] passwordArray = password.toString().toCharArray();
for (int i = 0; i < passwordArray.length; i++) {
int randomIndex = random.nextInt(passwordArray.length);
char temp = passwordArray[i];
passwordArray[i] = passwordArray[randomIndex];
passwordArray[randomIndex] = temp;
}
return new String(passwordArray);
}
public static void main(String[] args) {
int passwordLength = 12; // You can set this to any length you want
String password = generatePassword(passwordLength);
System.out.println("Generated Password: " + password);
}
}