2004/09/01

Your password is too short...


This is a random password generator that I stuck in my CryptUtils file. It's reasonable for website logins, etc.

private static final String VOWELS = "aeiou";
private static final String CONSONANTS = "bcdfghjklmnpqrstvwxyz";

private static transient SecureRandom random;

static {
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch(NoSuchAlgorithmException nsae) {
//... static logging here.
}
}

public static final String newPassword(int length) {

if(random == null) {
throw new RuntimeException("PRNG not initialised!");
}

if(length < 8) length = 8;

length -= 3;

char[] blabla = new char[length];

for(int i = 0; i < blabla.length; i+= 2) {
if(i >= blabla.length) break;
blabla[i] = CONSONANTS.charAt(random.nextInt(CONSONANTS.length()));
}

for(int i = 1; i < blabla.length; i+= 2) {
if(i >= blabla.length) break;
blabla[i] = VOWELS.charAt(random.nextInt(VOWELS.length()));
}

String result = new String(blabla);

if(result.indexOf("eni") != -1) {
return newPassword(length+3);
}

return result + Integer.toString(random.nextInt(999));
}