public String encodePassword(String password, Object salt) { try { return "{CRYPT}" + md5Crypt.crypt(password); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("No MD5 Algorithm", e); } }
public boolean isPasswordValid(String encPassword, String inputPassword, Object salt) { try { String encryptedPassword = encPassword; if (encryptedPassword.startsWith("{crypt}") || encryptedPassword.startsWith("{CRYPT}")) { encryptedPassword = encryptedPassword.substring("{crypt}".length()); } int lastDollar = encryptedPassword.lastIndexOf('$'); String realSalt = encryptedPassword.substring("$1$".length(), lastDollar); String check = md5Crypt.crypt(inputPassword, realSalt); return check.equals(encryptedPassword); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("No MD5 Algorithm", e); } }