KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > util > PasswordGenerator


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.util;
25
26 import java.util.Random JavaDoc;
27
28 /**
29  * Class that generates random strings suitable for use as passwords.
30  *
31  * @author Felix Gnass [fgnass at neteye dot de]
32  */

33 public class PasswordGenerator {
34
35     private static final String JavaDoc UPPER_CHARS = "ABCDEFGHJKLKMNPQRSTWXYZ";
36     private static final String JavaDoc AMBIGUOUS_UPPER_CHARS = "IOUV";
37     
38     private static final String JavaDoc LOWER_CHARS = "abcdefghijkmnpqrstwxyz";
39     private static final String JavaDoc AMBIGUOUS_LOWER_CHARS = "louv";
40     
41     private static final String JavaDoc DIGITS = "23456789";
42     private static final String JavaDoc AMBIGUOUS_DIGITS = "01";
43
44     private static PasswordGenerator defaultInstance = new PasswordGenerator();
45
46     private Random JavaDoc random = new Random JavaDoc();
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 JavaDoc 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 JavaDoc(
125                     "PasswordGenerator is already configured");
126         }
127     }
128     
129     protected void prepare() {
130         if (chars == null) {
131             chars = new StringBuffer JavaDoc();
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 JavaDoc(
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) (100 / (validPasswords / getPossibleCombinations()));
163
return (int)(getPossibleCombinations() / validPasswords);
164     }
165     
166     public String JavaDoc generate() {
167         prepare();
168         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(passwordLength);
169
170         // Loop through the password characters and assign each
171
// a random character from the set of allowed characters
172

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