📄 randpass.java
字号:
/* * Generate random passwords. * Copyright (C) 2001-2002 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * See COPYING.TXT for details. */package com.Ostermiller.util;import java.security.SecureRandom;import gnu.getopt.*;import java.text.MessageFormat;import java.util.ResourceBundle;import java.util.Locale;import java.util.Vector;/** * Generates a random String using a cryptographically * secure random number generator. * <p> * The alphabet (characters used in the passwords generated) * may be specified, and the random number generator can be * externally supplied. * <p> * Care should be taken when using methods that limit the types * of passwords may be generated. Using an alphabet that is too * small, using passwords that are too short, requiring too many * of a certain type of character, or not allowing repetition, * may decrease security. * <p> * More information about this class is available from <a target="_top" href= * "http://ostermiller.org/utils/RandPass.html">ostermiller.org</a>. * * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities * @since ostermillerutils 1.00.00 */public class RandPass { /** * Version number of this program * * @since ostermillerutils 1.00.00 */ public static final String version = "1.1"; /** * Locale specific strings displayed to the user. * * @since ostermillerutils 1.00.00 */ protected static ResourceBundle labels = ResourceBundle.getBundle("com.Ostermiller.util.RandPass", Locale.getDefault()); /** * Default length for passwords * * @since ostermillerutils 1.00.00 */ private static final int DEFAULT_PASSWORD_LENGTH = 8; /** * Alphabet consisting of upper and lowercase letters A-Z and * the digits 0-9. * * @since ostermillerutils 1.00.00 */ public static final char[] NUMBERS_AND_LETTERS_ALPHABET = { 'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v', 'w','x','y','z','0','1','2','3', '4','5','6','7','8','9', }; /** * Alphabet consisting of all the printable ASCII symbols. * * @since ostermillerutils 1.00.00 */ public static final char[] SYMBOLS_ALPHABET = { '!','\"','#','$','%','&','\'','(', ')','*','+',',','-','.','/',':', ';','<','?','@','[','\\',']','^', '_','`','{','|','}','~', }; /** * Alphabet consisting of all the printable ASCII characters. * * @since ostermillerutils 1.00.00 */ public static final char[] PRINTABLE_ALPHABET = { '!','\"','#','$','%','&','\'','(', ')','*','+',',','-','.','/','0', '1','2','3','4','5','6','7','8', '9',':',';','<','?','@','A','B', 'C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z', '[','\\',']','^','_','`','a','b', 'c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r', 's','t','u','v','w','x','y','z', '{','|','}','~', }; /** * Alphabet consisting of the lowercase letters A-Z. * * @since ostermillerutils 1.00.00 */ public static final char[] LOWERCASE_LETTERS_ALPHABET = { 'a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','p', 'q','r','s','t','u','v','w','x', 'y','z', }; /** * Alphabet consisting of the lowercase letters A-Z and * the digits 0-9. * * @since ostermillerutils 1.00.00 */ public static final char[] LOWERCASE_LETTERS_AND_NUMBERS_ALPHABET = { 'a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','p', 'q','r','s','t','u','v','w','x', 'y','z','0','1','2','3','4','5', '6','7','8','9', }; /** * Alphabet consisting of upper and lowercase letters A-Z. * * @since ostermillerutils 1.00.00 */ public static final char[] LETTERS_ALPHABET = { 'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v', 'w','x','y','z', }; /** * Alphabet consisting of the upper letters A-Z. * * @since ostermillerutils 1.00.00 */ public static final char[] UPPERCASE_LETTERS_ALPHABET = { 'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z', }; /** * Alphabet consisting of upper and lowercase letters A-Z and * the digits 0-9 but with characters that are often mistaken * for each other when typed removed. (I,L,O,U,V,i,l,o,u,v,0,1) * * @since ostermillerutils 1.00.00 */ public static final char[] NONCONFUSING_ALPHABET = { 'A','B','C','D','E','F','G','H', 'J','K','M','N','P','Q','R','S', 'T','W','X','Y','Z','a','b','c', 'd','e','f','g','h','j','k','m', 'n','p','q','r','s','t','w','x', 'y','z','2','3','4','5','6','7', '8','9', }; /** * Random number generator used. * * @since ostermillerutils 1.00.00 */ protected SecureRandom rand; /** * One less than the maximum number of repeated characters * that are allowed in a password. * Set to -1 to disable this feature. * * @since ostermillerutils 1.00.00 */ protected int repetition = -1; /** * Set of characters which may be * used in the generated passwords. * <p> * This value may not be null or have * no elements. * * @since ostermillerutils 1.00.00 */ protected char[] alphabet; /** * Set of characters which may be * used for the first character * in the generated passwords. * <p> * This value may be null but it mus * have at least one element otherwise. * * @since ostermillerutils 1.00.00 */ protected char[] firstAlphabet; /** * Set of characters which may be * used for the last character * in the generated passwords. * <p> * This value may be null but it mus * have at least one element otherwise. * * @since ostermillerutils 1.00.00 */ protected char[] lastAlphabet; /** * Create a new random password generator * with the default secure random number generator * and default NONCONFUSING alphabet for all characters. * * @since ostermillerutils 1.00.00 */ public RandPass(){ this(new SecureRandom(), NONCONFUSING_ALPHABET); } /** * Create a new random password generator * with the given secure random number generator * and default NONCONFUSING alphabet for all characters. * * @param rand Secure random number generator to use when generating passwords. * * @since ostermillerutils 1.00.00 */ public RandPass(SecureRandom rand){ this(rand, NONCONFUSING_ALPHABET); } /** * Create a new random password generator * with the default secure random number generator * and given alphabet for all characters. * * @param alphabet Characters allowed in generated passwords. * * @since ostermillerutils 1.00.00 */ public RandPass(char[] alphabet){ this(new SecureRandom(), alphabet); } /** * Create a new random password generator * with the given secure random number generator * and given alphabet for all characters. * * @param rand Secure random number generator to use when generating passwords. * @param alphabet Characters allowed in generated passwords. * * @since ostermillerutils 1.00.00 */ public RandPass(SecureRandom rand, char[] alphabet){ this.rand = rand; this.alphabet = alphabet; } private class Requirement { public Requirement(char[] alphabet, int num){ this.alphabet = alphabet; this.num = num; } public char[] alphabet; public int num; } /** * Generate a random passwords. * Run with --help argument for more information. * * @param args Command line arguments. * * @since ostermillerutils 1.00.00 */ public static void main(String[] args) throws Exception { // create the command line options that we are looking for LongOpt[] longopts = { new LongOpt(labels.getString("help.option"), LongOpt.NO_ARGUMENT, null, 1), new LongOpt(labels.getString("version.option"), LongOpt.NO_ARGUMENT, null, 2), new LongOpt(labels.getString("about.option"), LongOpt.NO_ARGUMENT, null, 3), new LongOpt(labels.getString("alphabet.option"), LongOpt.REQUIRED_ARGUMENT, null, 'a'), new LongOpt(labels.getString("first.alphabet.option"), LongOpt.REQUIRED_ARGUMENT, null, 'F'), new LongOpt(labels.getString("last.alphabet.option"), LongOpt.REQUIRED_ARGUMENT, null, 'L'), new LongOpt(labels.getString("number.option"), LongOpt.REQUIRED_ARGUMENT, null, 'n'), new LongOpt(labels.getString("maxrep.option"), LongOpt.REQUIRED_ARGUMENT, null, 'r'), new LongOpt(labels.getString("length.option"), LongOpt.REQUIRED_ARGUMENT, null, 'l'), new LongOpt(labels.getString("require.option"), LongOpt.REQUIRED_ARGUMENT, null, 'R'), new LongOpt(labels.getString("verify.option"), LongOpt.REQUIRED_ARGUMENT, null, 'v'), }; String oneLetterOptions = "a:n:F:L:r:l:R:v:"; Getopt opts = new Getopt(labels.getString("randpass"), args, oneLetterOptions, longopts); int number = 1; char[] alphabet = NONCONFUSING_ALPHABET; char[] firstAlphabet = null; char[] lastAlphabet = null; Vector reqs = new Vector(); Vector ver = new Vector(); int maxreps = 0; int length = 8; int c; while ((c = opts.getopt()) != -1){ switch(c){ case 1:{ // print out the help message String[] helpFlags = new String[]{ "--" + labels.getString("help.option"), "--" + labels.getString("version.option"), "--" + labels.getString("about.option"), "-a --" + labels.getString("alphabet.option") + " " + labels.getString("alphabet.argument"), "-n --" + labels.getString("number.option") + " " + labels.getString("number.argument"), "-F --" + labels.getString("first.alphabet.option") + " " + labels.getString("alphabet.argument"), "-L --" + labels.getString("last.alphabet.option") + " " + labels.getString("alphabet.argument"), "-l --" + labels.getString("length.option") + " " + labels.getString("number.argument"), "-r --" + labels.getString("maxrep.option") + " " + labels.getString("number.argument"), "-R --" + labels.getString("require.option") + " " + labels.getString("alphabet.argument"), "-v --" + labels.getString("verify.option") + " " + labels.getString("class.argument"), }; int maxLength = 0; for (int i=0; i<helpFlags.length; i++){ maxLength = Math.max(maxLength, helpFlags[i].length()); } maxLength += 2; System.out.println( labels.getString("randpass") + " [-" + StringHelper.replace(oneLetterOptions, ":", "") + "]\n" + labels.getString("purpose.message") + "\n" + " " + StringHelper.postpad(helpFlags[0] ,maxLength, ' ') + labels.getString("help.message") + "\n" + " " + StringHelper.postpad(helpFlags[1] ,maxLength, ' ') + labels.getString("version.message") + "\n" + " " + StringHelper.postpad(helpFlags[2] ,maxLength, ' ') + labels.getString("about.message") + "\n" + " " + StringHelper.postpad(helpFlags[3] ,maxLength, ' ') + labels.getString("a.message") + "\n" + " " + StringHelper.postpad(helpFlags[4] ,maxLength, ' ') + labels.getString("n.message") + "\n" + " " + StringHelper.postpad(helpFlags[5] ,maxLength, ' ') + labels.getString("F.message") + "\n" + " " + StringHelper.postpad(helpFlags[6] ,maxLength, ' ') + labels.getString("L.message") + "\n" + " " + StringHelper.postpad(helpFlags[7] ,maxLength, ' ') + labels.getString("l.message") + "\n" + " " + StringHelper.postpad(helpFlags[8] ,maxLength, ' ') + labels.getString("r.message") + "\n" + " " + StringHelper.postpad(helpFlags[9] ,maxLength, ' ') + labels.getString("R.message") + "\n" + " " + StringHelper.postpad(helpFlags[10] ,maxLength, ' ') + labels.getString("v.message") + "\n" ); System.exit(0); } break; case 2:{ // print out the version message System.out.println(MessageFormat.format(labels.getString("version"), new String[] {version})); System.exit(0); } break; case 3:{ System.out.println( labels.getString("randpass") + " -- " + labels.getString("purpose.message") + "\n" + MessageFormat.format(labels.getString("copyright"), new String[] {"2001-2002", "Stephen Ostermiller (http://ostermiller.org/contact.pl?regarding=Java+Utilities)"}) + "\n\n" + labels.getString("license") ); System.exit(0); } break; case 'a':{ String alph = opts.getOptarg(); if (alph.length() == 0){ alphabet = NONCONFUSING_ALPHABET; } else { alphabet = alph.toCharArray(); } } break; case 'F':{ String alph = opts.getOptarg(); if (alph.length() == 0){ firstAlphabet = null; } else { firstAlphabet = alph.toCharArray(); } } break; case 'L':{ String alph = opts.getOptarg(); if (alph.length() == 0){ lastAlphabet = null; } else { lastAlphabet = alph.toCharArray(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -