Pages

There are multiple ways to generate the MD5 hash in Java program. Not only Java API provides a convenient method for generating MD5 hash, you can also use popular open source frameworks like Spring and Apache commons Codec to generate MD5 digest in Java. MD5 is popular Message Digest Algorithm, which is most commonly used to check data integrity e.g. comparing MD5 checksum to see, if any file is altered or not. Though MD5 has not considered a good cryptographic algorithm for security purpose due to several vulnerabilities found on it, it's still good enough or checking the integrity of the file. MD5 hashing algorithm generates a 128 bit or 16-byte long hash value. MD5 hash values, also known as MD5 digest is mostly represented as 32 character Hex String. You can generate an MD5 hash from abyte array, or String directly using Java, Spring and Apache commons codec. Spring and Apache commons codec has identical API e.g. class name DigestUtils is same and allows you to directly generate MD5 hash as Hex String, while if you use Java then you need to convert byte array to Hex String, as java.security.MessageDigest.digest() method returns MD5 hash as a byte array. Earlier we have seen, How to encode and decode String in base64 encoding, and In this Java tutorial we will see, How to generate a MD5 hash or digest using Java, Spring and Apache commons code library.

How to Generate MD5 hash in Java - example

In this part, we will see some example to generate the MD5 hash in Java. Following Java program generates MD5 hash or digest of a String, by converting into a byte array. Java Security package provides MessageDigest, which can generate the MD5 hash. MessageDigest's digest() method accept a byte array and return a byte array of hash value. Since many times we need MD5 hash as Hex String, I have converted that byte array into Hex String. Apart from core Java example, which doesn't use any dependency, there are a couple of more open source solution of generating the MD5 digest. Two of the most popular open source library, Apache Commons Codec and Spring Framework provides utility method to create MD5 Hash in both byte array and Hex String format. Both Apache commons code and Spring provides, DigestUtils class with overloaded method md5() and md5Hex(), which can accept either String or byte array and can return either 16 element byte array or 32 characters Hex String.

If you are already using either Spring or Apache Commons Codec, then its best to use DigestUtil, as it only needs a line of code to generate the MD5 hash in Java. here is complete code example of generating MD5 digest in Java.

That's all on How to generate an MD5 hash or digest in Java. As you have seen, there is more than one way to generate a MD5 digest, both in a byte array as well as Hex String format. Just remember to specify file encoding to String.getBytes() method, until you have not used any application wide character encoding. Since String.getBytes() uses the platform specific encoding which could be different in your windows development machine and production Linux Server. Also, consider using Spring or Apache Commons Code to generate MD5 Hash value, if you are already using these libraries. It's always best to reuse library code than writing your own MD5 hash function, to avoid testing overhead.

3 comments
:

Hi Javin gr8 article fe things I want to add in this..The MessageDigest class can provide you with an instance of the MD5 digest.

Always when working with strings and the crypto classes be sure to always specify the encoding you want the byte representation in. If you just use string.getBytes() it will use the platform default. (Not all platforms use the same defaults)import java.security.*;

..

byte[] bytesOfMessage = yourString.getBytes("UTF-8");

MessageDigest md = MessageDigest.getInstance("MD5");byte[] thedigest = md.digest(bytesOfMessage);If you have a lot of data take a look at the .update(byte[]) method which can be called repeatedly. Then call .digest() to obtain the resulting hash.

@Saral, Indeed, specifying character encoding is very important, but, instead of hard coding them at code e.g. getBytes("UTF-8"), which works well on test programs, is not very convenient on real project. Consider using an application wide character encoding, which you can provide by using System property -Dfile.encoding="UTF-8". This allows you to change and manage character encoding easily. Regarding update(), yes that's preferred way if input is large.