MD5 Hash In Java
November 15, 2011
I needed to produce an MD5 hash in Java, in order to integrate with a credit card processing system.
This can be done with the following code:
byte[] lBytesOfMessage = lStringToEncode.toString().getBytes("UTF-8"); MessageDigest lMessageDigest = MessageDigest.getInstance("MD5"); byte[] lDigestBytes = lMessageDigest.digest(lBytesOfMessage);
To return this byte array as a hex string, the following function from commons-codec
can be used:
String lReturn = new String(Hex.encodeHex(lDigestBytes));
or even more simply:
String lReturn = Hex.encodeHexString(lDigestBytes);