2023-12-21 15:03:06 +08:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
class PasswordGenerator {
|
2023-12-21 16:39:49 +08:00
|
|
|
private static final int DEFAULT_PASSWORD_LENGTH = 3;
|
2023-12-21 15:03:06 +08:00
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try {
|
|
|
|
List<String> dictionary = loadDictionary(args[0]);
|
|
|
|
String password = generatePassword(dictionary, DEFAULT_PASSWORD_LENGTH);
|
|
|
|
System.out.println(password.replaceAll("o", "0"));
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("Failed to load dictionary file: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<String> loadDictionary(String filename) throws IOException {
|
|
|
|
List<String> dictionary = new ArrayList<>();
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
dictionary.add(line.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String generatePassword(List<String> dictionary, int length) {
|
|
|
|
StringBuilder passwordBuilder = new StringBuilder();
|
|
|
|
SecureRandom random;
|
|
|
|
try {
|
|
|
|
random = SecureRandom.getInstanceStrong();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
random = new SecureRandom();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
int randomIndex = random.nextInt(dictionary.size());
|
|
|
|
String word = dictionary.get(randomIndex);
|
2023-12-21 16:39:49 +08:00
|
|
|
passwordBuilder.append(toCamelCase(word));
|
2023-12-21 15:03:06 +08:00
|
|
|
if ( i < length-1) {
|
|
|
|
passwordBuilder.append("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return passwordBuilder.toString();
|
|
|
|
}
|
2023-12-21 16:39:49 +08:00
|
|
|
|
|
|
|
private static String toCamelCase(String inputString) {
|
|
|
|
return inputString.substring(0,1).toUpperCase() + inputString.substring(1).toLowerCase();
|
|
|
|
}
|
2023-12-21 15:03:06 +08:00
|
|
|
}
|