56 lines
1.9 KiB
Java
56 lines
1.9 KiB
Java
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 {
|
|
private static final int DEFAULT_PASSWORD_LENGTH = 3;
|
|
|
|
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);
|
|
passwordBuilder.append(toCamelCase(word));
|
|
if ( i < length-1) {
|
|
passwordBuilder.append("+");
|
|
}
|
|
}
|
|
return passwordBuilder.toString();
|
|
}
|
|
|
|
private static String toCamelCase(String inputString) {
|
|
return inputString.substring(0,1).toUpperCase() + inputString.substring(1).toLowerCase();
|
|
}
|
|
}
|