KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > passgen > BasePasswordGenerator


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.util.passgen;
15 import java.util.Random JavaDoc;
16
17 /**
18  * BasePasswordGenerator is a baseclass for generating random passwords.
19  * Inheriting classes should overload the constants USEDCHARS, MIN_CHARS
20  * and MAX_CHARS.
21  *
22  * @version $Id: BasePasswordGenerator.java,v 1.1 2006/01/17 20:28:05 anatom Exp $
23  */

24 public abstract class BasePasswordGenerator implements IPasswordGenerator{
25
26     protected BasePasswordGenerator(char[] usedchars){
27
28        this.usedchars = usedchars;
29     }
30
31     /**
32      * @see org.ejbca.util.passgen.IPasswordGenerator
33      */

34
35     public String JavaDoc getNewPassword(int minlength, int maxlength){
36         int difference = maxlength - minlength;
37         char[] password = null;
38
39         Random JavaDoc ran = new Random JavaDoc();
40
41         // Calculate the length of password
42
int passlen = maxlength;
43         if(minlength != maxlength)
44           passlen = minlength + ran.nextInt(difference);
45
46         password = new char[passlen];
47         for(int i=0; i < passlen; i++){
48           password[i] = usedchars[ran.nextInt(usedchars.length)];
49         }
50
51         return new String JavaDoc(password);
52     }
53
54
55     private final char[] usedchars;
56 }
57
Popular Tags