How do I calculate the MD5 digest of a string?

In this post you will learn how to calculate the MD5 digest of a string. One of the most commonly use for this functionality in an application is to secure a password. For security reasons, you will never want to store user passwords in the application database in a plain text. There are other more secure hash algorithm out there such as the SHA-1 (Secure Hash Algorithm) but the MD5 message digest offer a faster implementation compared to SHA-1.

You can calculate the MD5 digest using the message digest library provided by the Java Standard Edition library, but the Apache Commons Codec library gives you a simple and easy to use API for generating the MD5 hash. On the example below you will see how to generate to hash to secure a password and also to generate the hash for a longer text. The text is what you might want to send to a friend via an email. To make sure that your friend receive your original message without any modification you can send the generated hash in separated email that can be used to verify the message.

package org.kodejava.commons.codec;

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

public class MD5HashDemo {
    public static void main(String[] args) {
        // Calculates the MD5 digest for the password text and returns
        // the value as a 32 character hex string.
        String password = "s3cretw0rd**";
        String digest = DigestUtils.md5Hex(password);

        // Prints the plain text password, the digest and the length of
        // the digest.
        System.out.println("Password        = " + password);
        System.out.println("Password Digest = " + digest);
        System.out.println("Length          = " + digest.length());

        // Calculates the MD5 digest for the long texts.
        String md5 = """
                The MD5 message-digest algorithm is a formerly \
                widely used cryptographic hash function that produces \
                a 128-bit (16-byte) hash value. Specified in RFC 1321, \
                MD5 has been utilized in a wide variety of security \
                applications, and is also commonly used to check data \
                integrity. MD5 was designed by Ron Rivest in 1991 to \
                replace an earlier hash function, MD4. An MD5 hash value \
                is typically expressed as a hexadecimal number, 32 \
                digits long.
                """;
        String fingerprint = DigestUtils.md5Hex(md5);

        // Prints the text, the fingerprint and the length of the
        // digest / fingerprint.
        System.out.println("Text            = " + md5);
        System.out.println("Fingerprint     = " + fingerprint);
        System.out.println("Length          = " + fingerprint.length());
    }
}

As can be seen in the code example above, we use the DigestUtils.md5Hex() method to generate the MD5 digest. This class is part of the Apache Commons Codec under the org.apache.commons.codec.digest package. The method is a static method, so we don’t need to create an instance of the class before we can utilize the method. The md5Hex() method takes a string argument and produce a 32 characters hex string. The length will always 32 characters regardless the length of the processed text / string.

Besides, accepting a string argument, the overload version of the DigestUtils.md5Hex() method can accept an array of byte or a java.io.InputStream object as the argument.

Here is an example of the output produces:

Password        = s3cretw0rd**
Password Digest = 203c603a7330ab3ea032f4b9f140cf95
Length          = 32
Text            = The MD5 message-digest algorithm is a formerly widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. Specified in RFC 1321, MD5 has been utilized in a wide variety of security applications, and is also commonly used to check data integrity. MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4. An MD5 hash value is typically expressed as a hexadecimal number, 32 digits long.

Fingerprint     = a1e848ba8024fd5e38f567c107f0f4d2
Length          = 32

Maven Dependencies

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

Maven Central

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.