// 2012-09-12 johnpfeiffer
// TODO: password + salt
package net.kittyandbear.util;
import java.security.SecureRandom;
public class Password
{
private String password = "";
private static final String CLASSVERSION = "0.35";
private static String symbols = "aAbBcCdDeEfFGHJKLMnNPrRStTVWXYy023456789";
public static final int MINLENGTH = 1;
public static final int MAXLENGTH = 99;
public Password( int length )
{
if( length < MINLENGTH )
{
throw new IllegalArgumentException( "Length must be greater than " + MINLENGTH );
}
if( length > MAXLENGTH )
{
throw new IllegalArgumentException( "Length must be less than or equal to " + MAXLENGTH );
}
this.password = generateRandomString( length );
}
public static String getVersion()
{
return CLASSVERSION;
}
public String get()
{
return this.password;
}
public boolean isValid( String input )
{
boolean result = false;
if( input != null && !input.isEmpty() && input.equals( this.password ) ) // injection protection for <html/javascript>?
{
result = true;
}
return result;
}
private static String generateRandomString( int length )
{
StringBuffer stringbuffer = new StringBuffer();
SecureRandom random = new SecureRandom();
char c = 0;
for( int i = 0 ; i < length ; i++ )
{
int temp = random.nextInt( symbols.length() );
if( temp < symbols.length() && temp >= 0 )
{
c = symbols.charAt( temp );
stringbuffer.append( c );
}
}
return stringbuffer.toString();
}
} // end class