Possible Duplicate:
Creating a unique alphanumeric 10-character string
I need to generate unique random passwords.
So I thought of doing something like MD5(counter+a_random_string+timeInMills)
but this has a 32 chars output.
I need a 8 chars passwords.
How can generate unique custom length human-readable passwords?
Update:
I generate N (less that 30) passwords each time and I need these N password be non-redundant. The need of uniqueness is not absolute.
Also checking generated password for repeated value and re-generating it can cause an infinite loop
The brute-force way to guarantee uniqueness of randomly generated strings would be to store all of them and regenerate on collision:
protected Collection<String> generatedPasswords = new HashSet<String>();
public String generatePassword(int length) {
String password = null;
do {
StringBuilder buf = new StringBuilder();
// Append "length" random password characters to "buf".
password = buf.toString();
} while (this.generatedPasswords.contains(password));
return password;
}
However, if you're just trying to generate "reasonably" random passwords for end-users, and you're okay with the occasional (quite rare) collision then you could simply take the first N characters of your MD5 scheme.