// Get the user account
UNIXUser user = null;
try {
user = (UNIXUser) getAccount(username);
} catch (Exception e) {
throw new UserDatabaseException("Could not get user account", e);
}
// Make sure the user exists
if (user == null) {
throw new InvalidLoginCredentialsException();
}
// Determine the password type
String pw = new String(user.getPassword());
try {
if (pw.startsWith("$1$")) {
// MD5
return pw.substring(12).equals(MD5Crypt.crypt(password, pw.substring(3, 11)).substring(12));
} else if (pw.startsWith("$2a$")) {
// Blowfish
return BCrypt.checkpw(password, pw);
} else {
// DES
return DESCrypt.crypt(pw.substring(0, 2), password).equals(pw.substring(2));
}
} catch (Exception e) {
throw new UserDatabaseException("Invalid password format.", e);
}
}