diff --git a/Passgen/pom.xml b/Passgen/pom.xml new file mode 100644 index 0000000..5f1794a --- /dev/null +++ b/Passgen/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + com.enatheuer + Passgen + jar + 1.0-SNAPSHOT + Passgen + http://maven.apache.org + + + + info.picocli + picocli + 4.7.4 + + + + + src/main/java + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + + true + lib/ + Main + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + diff --git a/Passgen/src/main/java/Main.java b/Passgen/src/main/java/Main.java new file mode 100644 index 0000000..33e1504 --- /dev/null +++ b/Passgen/src/main/java/Main.java @@ -0,0 +1,95 @@ +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; + + @Option(names = {"-s", "--simple"}, description = "Will generate a password of specified length without special characters") + private boolean simple = false; + + @Option(names = {"-l", "--length"}, description = "Will generate a regular password with the provided amount of characters (or words for XKCD passwords). Defaults to 32.") + private int length = 32; + + @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 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() { + 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.exit(0); + } + else if (simple) { + System.out.println(generate_simple_password(length)); + System.exit(0); + } + else { + System.out.println(generate_regular_password(length)); + System.exit(0); + } + } +} diff --git a/Passgen/src/pom.xml b/Passgen/src/pom.xml new file mode 100644 index 0000000..5f1794a --- /dev/null +++ b/Passgen/src/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + com.enatheuer + Passgen + jar + 1.0-SNAPSHOT + Passgen + http://maven.apache.org + + + + info.picocli + picocli + 4.7.4 + + + + + src/main/java + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + + true + lib/ + Main + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + +