HOWTO · Java

How to Generate MD5 Hash in Java

Generate an MD5 digest in Java with explicit text encoding, hexadecimal output, and incremental updates.

On this page

This guide shows how to generate a repeatable MD5 digest in Java, convert it to lowercase hexadecimal, and process data in chunks. A digest is calculated from bytes. For text, choose a character encoding first so the same text becomes the same byte sequence on different systems. The example uses UTF-8 explicitly.

How MD5 Hashing Works in Java

MD5 maps an input byte sequence to a 128-bit (16-byte) digest, commonly displayed as 32 hexadecimal characters. Java’s MessageDigest class provides the digest API. Call getInstance() with the algorithm name, pass bytes to digest() for a one-step calculation, and format each returned byte as two hexadecimal digits. The hexadecimal string is a readable representation of the digest bytes; it is not the input text or an encryption of it.

Generate an MD5 Hash in Java

This complete program hashes the UTF-8 bytes of a fixed string. It appends two lowercase hexadecimal characters for every digest byte, including leading zeroes, so the result has a consistent width.

Run this example:

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

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String input = "Hello, Peter";
        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
        byte[] digest = MessageDigest.getInstance("MD5").digest(bytes);

        char[] hex = new char[digest.length * 2];
        char[] digits = "0123456789abcdef".toCharArray();
        for (int i = 0; i < digest.length; i++) {
            int value = digest[i] & 0xff;
            hex[i * 2] = digits[value >>> 4];
            hex[i * 2 + 1] = digits[value & 0x0f];
        }
        System.out.println(new String(hex));
    }
}
945062a2fee23e0901b37fcb5cd952c9

The program prints one 32-character lowercase hexadecimal digest. Because the input and encoding are fixed, running it again produces the same value. For another string, replace input; for arbitrary binary data, pass the original bytes directly instead of converting them to text.

Hash Large Inputs in Chunks

For data already arriving in pieces, create one MessageDigest, call update() with each byte array in its original order, then call digest() once after the final chunk. The accumulated input is equivalent to the concatenation of those arrays: omitting, duplicating, or reordering a chunk changes the bytes being hashed. If text is split before encoding, preserve the original character sequence and encode it consistently; preferably feed complete encoded byte chunks to update().

The no-argument digest() returns the digest of all bytes supplied since the instance was created or last reset. It also resets the instance for another calculation. Use a separate instance or deliberately start a new sequence for each independent value; do not mix bytes from separate inputs.

MD5 Security Limitations

MD5 has known collision weaknesses: an attacker can construct different inputs with the same digest. It is therefore unsuitable for passwords, digital signatures, or security-sensitive integrity checks where an attacker may alter data. A checksum published beside a file can detect accidental corruption when the expected value is trusted, but an MD5 value alone does not prove who created the file or prevent an attacker from replacing both the file and checksum. For adversarial integrity or authentication, choose a modern, purpose-appropriate mechanism and protect its key or expected value.

Conclusion

Use MessageDigest.getInstance("MD5") only when MD5 compatibility is required or for non-adversarial checks such as detecting accidental changes. Convert text to bytes with an explicit charset, render every digest byte consistently, and submit chunked bytes to update() in order before calling digest(). Treat the result as a checksum, not as encryption or proof of authenticity.