Java MD5 Hash using Apache Commons Codec

Tags: Apache Commons Codec Java MD5 Java Hash DigestUtils

In this Java tutorial we learn how to calculate MD5 digest in Java application using the DigestUtils class of Apache Commons Codec library.

Table of contents

  1. Add Apache Commons Codec Dependency to Java project
  2. Convert String to MD5 Hash in Java
  3. Generate File MD5 Checksum String in Java
  4. Generate MD5 Hash as Bytes Array
  5. Get Instance of java.security.MessageDigest for MD5 message digest algorithm

Add Apache Commons Codec Dependency to Java project

To use Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

To have more information about the Apache Commons Codec Java library you can visit the project home page at commons.apache.org/proper/commons-codec/

Convert String to MD5 Hash in Java

The Apache Commons Codec library provides method DigestUtils.md5Hex() can be used to calculate the MD5 digest and return value as a 32 characters hex string.

String md5Value = DigestUtils.md5Hex(inputData);

In the following Java program, we show how to use DigestUtils.md5Hex() method to hash an input String to MD5 hash as a hex string.

Md5Example1.java

import org.apache.commons.codec.digest.DigestUtils;

public class Md5Example1 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        String md5Value = DigestUtils.md5Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("MD5:" + md5Value);
    }
}
The output as below.
Input String:Simple Solution
MD5:6cd04c53878b462e5d7d400a11ac19cf

Generate File MD5 Checksum String in Java

Apache Commons Codec library provides the DigestUtils.md5Hex() method to generate MD5 hash for an InputStream as below.

String md5Value = DigestUtils.md5Hex(inputStream);

For example, we have a document file at D:\SimpleSolution\Document.doc

The following Java program to show you how to use the DigestUtils.md5Hex() to generate MD5 checksum hash for a given file and return MD5 hash value as a hex string.

Md5Example2.java

import org.apache.commons.codec.digest.DigestUtils;

import java.io.FileInputStream;
import java.io.IOException;

public class Md5Example2 {
    public static void main(String... args) throws IOException {
        String filePath = "D:\\SimpleSolution\\Document.docx";

        String md5Value = DigestUtils.md5Hex(new FileInputStream(filePath));

        System.out.println("MD5:" + md5Value);
    }
}
The output as below.
MD5:b3143672f91aea6ceceae116a852d4d6

Generate MD5 Hash as Bytes Array

The Apache Commons Codec library also provides DigestUtils.md5() method to calculate MD5 digest and return value as a 16 element byte[] array.

byte[] md5Value = DigestUtils.md5(inputData);

The following Java example program to show you how to use the DigestUtils.md5() method to hash a String to a MD5 hash value as byte[] array.

Md5Example3.java

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;

public class Md5Example3 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        byte[] md5Value = DigestUtils.md5(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("MD5 as bytes:" + Arrays.toString(md5Value));
    }
}
The output as below.
Input String:Simple Solution
MD5 as bytes:[108, -48, 76, 83, -121, -117, 70, 46, 93, 125, 64, 10, 17, -84, 25, -49]

Get Instance of java.security.MessageDigest for MD5 message digest algorithm

Using Apache Commons Codec we can use the DigestUtils.getMd5Digest() method to return instance of MD5 java.security.MessageDigest and use it to calculate MD5 digest as following Java program.

Md5Example4.java

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class Md5Example4 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        MessageDigest md5MessageDigest = DigestUtils.getMd5Digest();
        byte[] hashedBytes = md5MessageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
        String md5Value = Hex.encodeHexString(hashedBytes);

        System.out.println("Input String:" + inputData);
        System.out.println("MD5:" + md5Value);
    }
}
The output as below.
Input String:Simple Solution
MD5:6cd04c53878b462e5d7d400a11ac19cf

Happy Coding 😊

Java MD2 Hash using Apache Commons Codec

Java SHA-256 Hash using Apache Commons Codec

Java SHA-384 Hash using Apache Commons Codec

Java SHA-512 Hash using Apache Commons Codec

Java SHA3-224 Hash using Apache Commons Codec

Java SHA3-256 Hash using Apache Commons Codec

Java SHA3-384 Hash using Apache Commons Codec

Java SHA3-512 Hash using Apache Commons Codec

Java SHA-512 / 224 Hash using Apache Commons Codec

Java SHA-512 / 256 Hash using Apache Commons Codec

Java Generate SHA-1 using DigestUtils in Apache Commons Codec

Java Apache Commons Codec Tutorial