Andrew Andrew - 2 months ago 12
Java Question

Convert MD5 array to String java

I know that there is a lot of similar topics, but still... can someone provide me a working example of method which generates MD5 String.

I'm currently using MessageDigest, and I'm doing the following to get a string

sun.misc.BASE64Encoder().encode(messageDigest.digest())


I guess there is some better way to do that.

Thanks in advance!

Answer
import java.math.*;
import java.security.*;

String toEnc = "Hash This!"; // Value to hash
MessageDigest mdEnc = MessageDigest.getInstance("MD5"); 
mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
String md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Hash value