public String generateRandomPassword() {
/* TODO: keep this in sync with the security requirements */
String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String LOWER = "abcdefghijklmnopqrstuvwxyz";
String DIGITS = "0123456789";
String SYMBOLS = "!@#$%&*";
String ALL_CHARS = UPPER + LOWER + DIGITS + SYMBOLS;
/* Generate the password char by char while ensuring every
char category is represented */
StringBuilder password = new StringBuilder(32);
/* Using a SecureRandom is required to avoid attacker
guessing the generated password */
SecureRandom random = new SecureRandom();
for (int i = 0; i < 32; i++) {
int idx = random.nextInt(ALL_CHARS.length());
password.append(ALL_CHARS.charAt(idx));
}
return password.toString();
}