KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > PasswordGenerator


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 import org.mmbase.util.logging.Logger;
14 import org.mmbase.util.logging.Logging;
15
16 /**
17  * Module for the automatic generation of passwords.
18  * Based on the code of Arnold G. Reinhold, Cambridge, MA, USA.
19  * This code is under the GNU License as specified by the original author.
20  * <br />
21  * The passwords generated by this class are based on a template.
22  * A template can exists of a number of characters, which are replaced by
23  * the generator as follows:<br />
24  * A : is replaced by a random letter (a - z).<br />
25  * C : is replaced by a random alphanumeric character (0-9, a-z)<br />
26  * H : is replaced by a random hex character (0-9,A-F)<br />
27  * S : is replaced by a random syllable. This, alterating, an element from a set of
28  * consonants or an element form a set of vowels. A syllable can be more than
29  * one character (i.e. "qu").<br />
30  * 6 : is replaced by a random dice-roll (1-6)<br />
31  * 9 : is replaced by a random digit (0-9)<br />
32  * <br />
33  *
34  * @license uses the GNU license, should be moved external
35  * @author Rico Jansen
36  * @author Pierre van Rooden (javadocs)
37  * @version $Id: PasswordGenerator.java,v 1.11 2005/12/18 00:19:21 michiel Exp $
38  */

39
40 public class PasswordGenerator implements PasswordGeneratorInterface {
41
42     private static final Logger log = Logging.getLoggerInstance(PasswordGenerator.class);
43
44
45     /**
46      * List of consonants that can be used in a password.
47      */

48     private static String JavaDoc consonants[] = {
49             "b","c","d","f","g","h","j","k","l","m",
50             "n","p","qu","r","s","t","v","w","x","z",
51             "ch","cr","fr","nd","ng","nk","nt","ph","pr","rd",
52             "sh","sl","sp","st","th","tr"
53         };
54
55     /**
56      * List of vowels that can be used in a password.
57      */

58     private static String JavaDoc vowels[] = { "a","e","i","o","u","y" };
59
60     /**
61      * Pool of random numbers.
62      */

63     RandomPool ranPool;
64
65     /**
66      * Default template to use when generating passwords.
67      */

68     String JavaDoc defaulttemplate = "SSSSSS";
69
70     /**
71      * Creates the generator
72      */

73     public PasswordGenerator() {
74         ranPool = new RandomPool();
75     }
76
77     /**
78      * Entry point when calling from teh command line.
79      * Used for testing.
80      */

81     public static void main(String JavaDoc args[]) {
82         PasswordGenerator PG = new PasswordGenerator();
83         log.info("Password "+PG.getPassword());
84         log.info("Password "+PG.getPassword("SSS 9 SSS"));
85         log.info("Password "+PG.getPassword("SSSS"));
86         log.info("Password "+PG.getPassword("SSSSS"));
87         log.info("Password "+PG.getPassword("SSSSSS"));
88         log.info("Password "+PG.getPassword("CCC CCC CCC"));
89         log.info("Password "+PG.getPassword("HHHH HHHH HHHH"));
90         log.info("Password "+PG.getPassword("AAAAA AAAAA AAAAA"));
91         log.info("Password "+PG.getPassword("99999 99999 99999"));
92         log.info("Password "+PG.getPassword("66666 66666 66666"));
93     }
94
95     /**
96      * Returns the positive modulus of the arguments.
97      * @param x the argument to modulate
98      * @param y the argument to modulate with
99      * @return the positive modulus of x modulus y.
100      */

101     private int mod(long x, long y) {
102         if (x<0) x=-x;
103         if (y<0) y=-y;
104         return (int) (x % y);
105     }
106
107     /**
108      * Generate a password, based on the default template for this module.
109      * @return the generated password.
110      */

111     public String JavaDoc getPassword() {
112         return getPassword(defaulttemplate);
113     }
114
115     /**
116      * Generate a password, based on the given template.
117      * @param template the template the password should be based on.
118      * @return the generated password.
119      */

120     public String JavaDoc getPassword(String JavaDoc template) {
121         int len;
122         boolean next = true;
123         StringBuffer JavaDoc pwd = new StringBuffer JavaDoc();
124
125         len=template.length();
126         for (int i=0;i<len;i++) {
127             ranPool.stir(i*93762+49104); // Increasing value
128
next=addChar(pwd,template,i,next);
129         }
130         return(pwd.toString());
131     }
132
133     /**
134      * Add a random character to the password as specified by the template.
135      * @param password the <code>StringBuffer</code> containing the password to add a new character to
136      * @param template template to use when generating the password
137      * @param ntmpl index in the template indicating the template character to use
138      * @param consonantNext if <code>true</code>, the 'S' template character will
139      * return a consonant. otherwise it will return a vowel.
140      * @return <code>false</code> if the value returned was a conmsonant generated by the 'S'
141      * template character, otherwise <code>true</code>.
142      */

143     private boolean addChar(StringBuffer JavaDoc password,String JavaDoc template, int ntmpl,boolean consonantNext) {
144         int ch = 0;
145         char tmplChar;
146         String JavaDoc charsOut=null;
147
148         if (ntmpl >= template.length()) {
149             consonantNext = true;
150             return consonantNext;
151         }
152         tmplChar = template.charAt(ntmpl);
153
154         if (tmplChar == ' ') {
155             ch = ' ';
156
157         } else if (tmplChar == 'A') { //random letter
158
ch = mod(ranPool.value(), 26) + (int) 'a';
159
160         } else if (tmplChar == 'C') { //random alphanumeric [0-9,A-Z]
161
ch = mod(ranPool.value(), 36);
162             if (ch <10) ch = ch + (int) '0';
163             else ch = ch + (int) 'a' - 10;
164
165         } else if (tmplChar == 'H') { //random hex digit
166
ch = mod(ranPool.value(), 16);
167             if (ch <10) ch = ch + (int) '0';
168             else ch = ch + (int) 'a' - 10;
169
170         } else if (tmplChar == 'S') { //random syllable
171
if (consonantNext) {
172                 charsOut = consonants[mod(ranPool.value(), consonants.length)];
173                 if (!"qu".equals(charsOut)) consonantNext = false;
174             } else {
175                 charsOut = vowels[mod(ranPool.value(), vowels.length)];
176                 consonantNext = true;
177             }
178
179         } else if (tmplChar == '6') { //random dice throw
180
ch = mod(ranPool.value(), 6) + (int) '1';
181
182         } else if (tmplChar == '9') { //random digit
183
ch = mod(ranPool.value(), 10) + (int) '0';
184
185         } else {
186             return consonantNext;
187         }
188
189         if (charsOut==null) {
190             charsOut = String.valueOf((char)ch);
191             consonantNext = true;
192         }
193         password.append(charsOut);
194         return consonantNext;
195     }
196
197     /**
198      * Returns a description of the module.
199      */

200     public String JavaDoc getModuleInfo() {
201         return("Password Generator module, based on the code of Arnold G. Reinhold, Cambridge, MA, USA. Author Rico Jansen");
202     }
203 }
204
205
Popular Tags