Split up into 2 files for better structure
This commit is contained in:
parent
5c08b4e536
commit
7e3b23eb88
2 changed files with 65 additions and 56 deletions
|
@ -1,13 +1,7 @@
|
|||
import java.security.SecureRandom;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Option;
|
||||
|
||||
public class Main implements Runnable {
|
||||
private static SecureRandom random;
|
||||
|
||||
@Option(names = {"-x", "--xkcd"}, description = "Will generate an xkcd-style password (See xkcd 936)")
|
||||
private boolean xkcd = false;
|
||||
|
@ -21,74 +15,28 @@ public class Main implements Runnable {
|
|||
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Displays this help message and exits.")
|
||||
private boolean help;
|
||||
|
||||
private static String generate_simple_password(int count) {
|
||||
String simpleCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
String password = "";
|
||||
int randomIndex;
|
||||
char randomChar;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(simpleCharacters.length());
|
||||
randomChar = simpleCharacters.charAt(randomIndex);
|
||||
password = password + randomChar;
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
private static String generate_regular_password(int count) {
|
||||
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*";
|
||||
String password = "";
|
||||
int randomIndex;
|
||||
char randomChar;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(characters.length());
|
||||
randomChar = characters.charAt(randomIndex);
|
||||
password = password + randomChar;
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
private static String generate_xkcd_password(int count) {
|
||||
String password = "";
|
||||
try {
|
||||
List<String> dictionary = Files.readAllLines(Paths.get("/usr/share/dict/words"));
|
||||
|
||||
int randomIndex;
|
||||
String randomWord;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(dictionary.size());
|
||||
randomWord = dictionary.get(randomIndex);
|
||||
password = password + randomWord;
|
||||
password = password + " ";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("An error occured while reading /usr/share/dict/words - Is the file present?");
|
||||
System.exit(2);
|
||||
}
|
||||
return password;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
random = new SecureRandom();
|
||||
int exitCode = new CommandLine(new Main()).execute(args);
|
||||
System.exit(exitCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final Passgen passgen = new Passgen();
|
||||
if (xkcd && simple) {
|
||||
System.out.println("Error: Only use one of xkcd (-x), simple (-s) or regular (-r) at once.");
|
||||
System.exit(2);
|
||||
}
|
||||
if (xkcd) {
|
||||
System.out.println(generate_xkcd_password(length));
|
||||
System.out.println(passgen.generate_xkcd_password(length));
|
||||
System.exit(0);
|
||||
}
|
||||
else if (simple) {
|
||||
System.out.println(generate_simple_password(length));
|
||||
System.out.println(passgen.generate_simple_password(length));
|
||||
System.exit(0);
|
||||
}
|
||||
else {
|
||||
System.out.println(generate_regular_password(length));
|
||||
System.out.println(passgen.generate_regular_password(length));
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
|
61
Passgen/src/main/java/Passgen.java
Normal file
61
Passgen/src/main/java/Passgen.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
import java.security.SecureRandom;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
public class Passgen {
|
||||
private static SecureRandom random;
|
||||
|
||||
|
||||
public String generate_simple_password(int count) {
|
||||
random = new SecureRandom();
|
||||
String simpleCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
String password = "";
|
||||
int randomIndex;
|
||||
char randomChar;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(simpleCharacters.length());
|
||||
randomChar = simpleCharacters.charAt(randomIndex);
|
||||
password = password + randomChar;
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
public String generate_regular_password(int count) {
|
||||
random = new SecureRandom();
|
||||
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*";
|
||||
String password = "";
|
||||
int randomIndex;
|
||||
char randomChar;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(characters.length());
|
||||
randomChar = characters.charAt(randomIndex);
|
||||
password = password + randomChar;
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
public String generate_xkcd_password(int count) {
|
||||
random = new SecureRandom();
|
||||
String password = "";
|
||||
try {
|
||||
List<String> dictionary = Files.readAllLines(Paths.get("/usr/share/dict/words"));
|
||||
|
||||
int randomIndex;
|
||||
String randomWord;
|
||||
for (int i = 0; i < count; i++) {
|
||||
randomIndex = random.nextInt(dictionary.size());
|
||||
randomWord = dictionary.get(randomIndex);
|
||||
password = password + randomWord;
|
||||
password = password + " ";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("An error occured while reading /usr/share/dict/words - Is the file present?");
|
||||
System.exit(2);
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue