MD5 Hashing in Java
2.MD5 Using MessageDigest Class
java.security.MessageDigest class:
MessageDigest.getInstance(String Algorithm)
public void update(byte [] input)
public byte[] digest()
generates a hash for a password and then verifies it:
@Test
public void givenPassword_whenHashing_thenVerifying()
throws NoSuchAlgorithmException {
String hash = "35454B055CC325EA1AF2126E27707052";
String password = "ILoveJava";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
String myHash = DatatypeConverter
.printHexBinary(digest).toUpperCase();
assertThat(myHash.equals(hash)).isTrue();
}