Nov 23, 2010

Generate random passwords

Hi,
Here is a simple way with no effort to create random passwords like with the characters you want.
You will need this member:
private static final String charset = "aAbBcCdD$#eEf!=m2MnN3oOp4P8VwWx9XyY0zZ";
And this method:
public String getPasswordString(int length) {
Random randomSeed = new Random(System.currentTimeMillis());
StringBuilder result= new StringBuilder();
for (int i = 0; i < length; i++) {
int pos = randomSeed .nextInt(charset.length());
result.append(charset.charAt(pos));
}
return result.toString();
}
Of course there is another way by using the UUID object:
UUID.randomUUID().toString();
Or maybe using an external third party library.
Hoped i helped,
Dor

No comments:

Post a Comment