How to generate an MD5 hash from a string in Java?

What is the best way to generate an MD5 hash in Java? How can I create an MD5 hash from a string using Java MD5 functionality? Is there a built-in method in Java to achieve this efficiently?

I’ve worked with Java security libraries for years, and the built-in MessageDigest class is my go-to solution when I want a native, dependency-free way to generate MD5 hashes.

Using MessageDigest (Recommended & Built-in) If you’re looking for a native Java solution without third-party libraries, MessageDigest is the way to go.

Here’s how you can generate a Java MD5 hash:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;

public class MD5Example {
    public static String getMD5Hash(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte[] digest = md.digest();
            BigInteger no = new BigInteger(1, digest);
            return String.format("%032x", no);  // 32-character hex string
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println(getMD5Hash("HelloWorld"));  // Example usage
    }
}

:small_blue_diamond: Why this is great?

:white_check_mark: Built into Java → No external dependencies.

:white_check_mark: Produces a consistent 32-character Java MD5 hash.

:white_check_mark: Reliable and widely supported.

When working on large-scale projects, I often prefer using external libraries like Apache Commons Codec—it makes the code concise and cleaner.

Using Apache Commons Codec (Easier & Cleaner Code) If you want something simpler, the Apache Commons Codec library offers a clean one-liner way to generate an MD5 hash.

First, add the Maven dependency:

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

Then, generate the Java MD5 hash with just one line of code:

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

public class MD5ApacheExample {
    public static void main(String[] args) {
        String hash = DigestUtils.md5Hex("HelloWorld");
        System.out.println(hash);  // Prints MD5 hash
    }
}

:small_blue_diamond: Why this is great?

:white_check_mark: Super easy—just one line of code.

:white_check_mark: Automatically handles encoding.

:white_check_mark: Uses the widely trusted Apache Commons library.

If you’re already using Guava in your project, why not leverage its hashing utilities? It offers a clean and readable way to generate MD5 hashes."*

Using Guava (Google’s Library) If your project already uses Google Guava, you can use its Hashing class to generate Java MD5 hashes effortlessly.

First, add the Maven dependency:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

Then, use this code:

import com.google.common.hash.Hashing;
import java.nio.charset.StandardCharsets;

public class MD5GuavaExample {
    public static void main(String[] args) {
        String hash = Hashing.md5()
                .hashString("HelloWorld", StandardCharsets.UTF_8)
                .toString();
        System.out.println(hash);
    }
}

:small_blue_diamond: Why this is great?

:white_check_mark: Simple & readable syntax.

:white_check_mark: Ideal if you’re already using Google Guava.

:white_check_mark: Convenient for working with multiple hashing algorithms.