1 10 11 package org.mmbase.util; 12 13 import org.mmbase.util.logging.Logger; 14 import org.mmbase.util.logging.Logging; 15 16 39 40 public class PasswordGenerator implements PasswordGeneratorInterface { 41 42 private static final Logger log = Logging.getLoggerInstance(PasswordGenerator.class); 43 44 45 48 private static String 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 58 private static String vowels[] = { "a","e","i","o","u","y" }; 59 60 63 RandomPool ranPool; 64 65 68 String defaulttemplate = "SSSSSS"; 69 70 73 public PasswordGenerator() { 74 ranPool = new RandomPool(); 75 } 76 77 81 public static void main(String 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 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 111 public String getPassword() { 112 return getPassword(defaulttemplate); 113 } 114 115 120 public String getPassword(String template) { 121 int len; 122 boolean next = true; 123 StringBuffer pwd = new StringBuffer (); 124 125 len=template.length(); 126 for (int i=0;i<len;i++) { 127 ranPool.stir(i*93762+49104); next=addChar(pwd,template,i,next); 129 } 130 return(pwd.toString()); 131 } 132 133 143 private boolean addChar(StringBuffer password,String template, int ntmpl,boolean consonantNext) { 144 int ch = 0; 145 char tmplChar; 146 String 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') { ch = mod(ranPool.value(), 26) + (int) 'a'; 159 160 } else if (tmplChar == 'C') { 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') { 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') { 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') { ch = mod(ranPool.value(), 6) + (int) '1'; 181 182 } else if (tmplChar == '9') { 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 200 public String 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 |