Java tutorial
// Copyright (c) Andrew Smith. All rights reserved. // The use and distribution terms for this software are covered by the // Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) // which can be found in the file epl-v10.html at the root of this distribution. // By using this software in any fashion, you are agreeing to be bound by // the terms of this license. // You must not remove this notice, or any other, from this software. package org.jpwgen; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.List; import java.util.Random; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; import org.apache.commons.cli.CommandLine; public class Main { public static void main(String[] args) throws NoSuchAlgorithmException { CommandLineParser parser = new PosixParser(); HelpFormatter formatter = new HelpFormatter(); Options options = new Options(); options.addOption("l", "lowercase", false, "Include lowercase alpha characters"); options.addOption("u", "uppercase", false, "Include uppercase alpha characters"); options.addOption("d", "digits", false, "Include digits"); options.addOption("p", "punctuation", false, "Include punctuation"); options.addOption("n", "number", true, "Number of passwords to generate"); options.addOption("L", "length", true, "Length of the generated passwords"); try { CommandLine cmd = parser.parse(options, args); boolean hasLower = cmd.hasOption('l'); boolean hasUpper = cmd.hasOption('u'); boolean hasDigits = cmd.hasOption('d'); boolean hasPunctuation = cmd.hasOption('p'); boolean hasLength = cmd.hasOption('L'); boolean hasNumber = cmd.hasOption('n'); // Check for options if (hasLower || hasUpper || hasDigits || hasPunctuation) { PasswordGenerator generator; generator = new PasswordGenerator(SecureRandom.getInstance("SHA1PRNG")); PasswordChars passwordChars = new PasswordChars(hasLower, hasUpper, hasDigits, hasPunctuation); // Number of passwords to generate by default int number = 10; // Length of passwords generated by default int length = 10; // If length has been specified by the user, use it // in place of the default value. if (hasLength) { try { length = Integer.parseInt(cmd.getOptionValue('L')); } catch (NumberFormatException e) { System.err.println("Error: The argument to length must be a number"); formatter.printHelp("pwgen", options); System.exit(-1); } } // If number has been specified by the user, use it // in place of the default value. if (hasNumber) { try { number = Integer.parseInt(cmd.getOptionValue('n')); } catch (NumberFormatException e) { System.err.println("Error: The argument to number must be a number"); formatter.printHelp("pwgen", options); System.exit(-1); } } final List<String> passwords; passwords = generator.generateMultiple(passwordChars, number, length); for (String password : passwords) { System.out.println(password); } } else { // If none found, print the usage message. formatter.printHelp("pwgen", options); } } catch (ParseException e) { System.err.println("Error parsing the commandline: " + e.getMessage()); } } }