1 24 package org.riotfamily.common.util; 25 26 import java.util.Random ; 27 28 33 public class PasswordGenerator { 34 35 private static final String UPPER_CHARS = "ABCDEFGHJKLKMNPQRSTWXYZ"; 36 private static final String AMBIGUOUS_UPPER_CHARS = "IOUV"; 37 38 private static final String LOWER_CHARS = "abcdefghijkmnpqrstwxyz"; 39 private static final String AMBIGUOUS_LOWER_CHARS = "louv"; 40 41 private static final String DIGITS = "23456789"; 42 private static final String AMBIGUOUS_DIGITS = "01"; 43 44 private static PasswordGenerator defaultInstance = new PasswordGenerator(); 45 46 private Random random = new Random (); 47 48 private boolean useUpperChars; 49 private boolean useLowerChars; 50 private boolean useDigits; 51 52 private boolean includeAmbiguous; 53 54 private int passwordLength; 55 56 private StringBuffer chars; 57 58 public PasswordGenerator() { 59 this(8, true, true, true); 60 } 61 62 public PasswordGenerator( 63 int passwordLength, 64 boolean useUpperChars, 65 boolean useLowerChars, 66 boolean useDigits) { 67 68 this.passwordLength = passwordLength; 69 this.useUpperChars = useUpperChars; 70 this.useLowerChars = useLowerChars; 71 this.useDigits = useDigits; 72 } 73 74 public static PasswordGenerator getDefaultInstance() { 75 return defaultInstance; 76 } 77 78 public boolean getUseUpperChars() { 79 return useUpperChars; 80 } 81 82 public boolean getUseLowerChars() { 83 return useLowerChars; 84 } 85 86 public boolean getUseDigits() { 87 return useDigits; 88 } 89 90 public boolean isIncludeAmbiguous() { 91 return includeAmbiguous; 92 } 93 94 public void setUseUpperChars(boolean v) { 95 check(); 96 useUpperChars = v; 97 } 98 99 public void setUseLowerChars(boolean v) { 100 check(); 101 useLowerChars = v; 102 } 103 104 public void setUseDigits(boolean v) { 105 check(); 106 useDigits = v; 107 } 108 109 public void setIncludeAmbiguous(boolean includeAmbiguous) { 110 check(); 111 this.includeAmbiguous = includeAmbiguous; 112 } 113 114 public void setPasswordLength(int i) { 115 passwordLength = i; 116 } 117 118 public int getPasswordLength() { 119 return passwordLength; 120 } 121 122 protected void check() { 123 if (chars != null) { 124 throw new IllegalStateException ( 125 "PasswordGenerator is already configured"); 126 } 127 } 128 129 protected void prepare() { 130 if (chars == null) { 131 chars = new StringBuffer (); 132 if (getUseLowerChars()) { 133 chars.append(LOWER_CHARS); 134 if (includeAmbiguous) { 135 chars.append(AMBIGUOUS_LOWER_CHARS); 136 } 137 } 138 if (getUseUpperChars()) { 139 chars.append(UPPER_CHARS); 140 if (includeAmbiguous) { 141 chars.append(AMBIGUOUS_UPPER_CHARS); 142 } 143 } 144 if (getUseDigits()) { 145 chars.append(DIGITS); 146 if (includeAmbiguous) { 147 chars.append(AMBIGUOUS_DIGITS); 148 } 149 } 150 if (chars.length() == 0) { 151 throw new IllegalStateException ( 152 "At least one type of characters must be specified"); 153 } 154 } 155 } 156 157 public long getPossibleCombinations() { 158 return (long) Math.pow(chars.length(),passwordLength); 159 } 160 161 public int getChanceToGuess(long validPasswords) { 162 return (int)(getPossibleCombinations() / validPasswords); 164 } 165 166 public String generate() { 167 prepare(); 168 StringBuffer buffer = new StringBuffer (passwordLength); 169 170 173 for (int i = 0; i < passwordLength; i++) { 174 int index = random.nextInt() % chars.length(); 175 index = Math.abs(index); 176 char c = chars.charAt(index); 177 buffer.append(c); 178 } 179 return buffer.toString(); 180 } 181 182 } 183 | Popular Tags |