Using DigestUtils.sha1hex() method to generate SHA-1 digest

In this example you’ll learn how to generate an SHA-1 digest using the Apache Commons Codec DigestUtils class. In the last two examples you’ve already seen how to generate the MD5 digest using the same library. Compared to the MD5 version the SHA-1 digest is known to be stronger to brute force attacks, but it slower to generate. The SHA-1 produces a 160 bit (20 byte) message digest while the MD5 produces only a 128 bit message digest (16 byte).

In the code snippet below we demonstrate three different ways to use the DigestUtils.sha1Hex() method. In the first method in the example, the byteDigest(), we calculates the digest from an array of byte data. Followed by the second method, the inputStreamDigest() where we calculate the digest of an InputStream object. And on the last method we call the overload version of the sha1Hex() method to calculate the digest of a string.

Let’s see the full code snippet.

package org.kodejava.example.commons.codec;

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

import java.io.*;

public class SHAHashDemo {
    public static void main(String[] args) {
        SHAHashDemo demo = new SHAHashDemo();
        demo.byteDigest();
        demo.inputStreamDigest();
        demo.stringDigest();
    }

    /**
     * Calculates SHA-1 digest from byte array.
     */
    private void byteDigest() {
        System.out.println("SHAHashDemo.byteDigest");
        try {
            byte[] data = "The quick brown fox jumps over the lazy dog.".getBytes("UTF-8");
            String digest = DigestUtils.sha1Hex(data);
            System.out.println("Digest          = " + digest);
            System.out.println("Digest.length() = " + digest.length());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * Calculates SHA-1 digest of InputStream object.
     */
    private void inputStreamDigest() {
        System.out.println("SHAHashDemo.inputStreamDigest");
        String data = System.getProperty("user.dir") + "/target/classes/data.txt";
        File file = new File(data);
        try {
            InputStream is = new FileInputStream(file);
            String digest = DigestUtils.sha1Hex(is);
            System.out.println("Digest          = " + digest);
            System.out.println("Digest.length() = " + digest.length());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Calculate SHA-1 digest of a string / text.
     */
    private void stringDigest() {
        System.out.println("SHAHashDemo.stringDigest");
        String data = "This is just a simple data message for SHA digest demo.";
        String digest = DigestUtils.sha1Hex(data);
        System.out.println("Digest          = " + digest);
        System.out.println("Digest.length() = " + digest.length());
    }
}

When you run the code it will output the following result:

SHAHashDemo.stringDigest
Digest          = 4290d13ca383c2159c442d75355d83e310a2ea15
Digest.length() = 40
SHAHashDemo.inputStreamDigest
Digest          = b94d7f261acd677ae69f4244e5f894313a2cd559
Digest.length() = 40
SHAHashDemo.byteDigest
Digest          = 408d94384216f890ff7a0c3528e8bed1e0b01621
Digest.length() = 40

Generating MD5 digest from File or InputStream object

In the previous example you can see how to calculate the MD5 digest from a text or a string. We are using the Apache Commons Codec library and use the DigestUtils.md5Hex() method to generate the MD5. I’ve mention in that post that we can also generate the MD5 digest of a byte array and InputStream object. In the example below you’ll see an example to generate the MD5 digest of a text data stored in a file.

package org.kodejava.example.commons.codec;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MD5FileHashDemo {
    public static void main(String[] args) {
        try {
            // Define the data file path and create an InputStream object.
            String data = System.getProperty("user.dir") + "/target/classes/data.txt" ;
            File file = new File(data);
            InputStream is = new FileInputStream(file);

            // Calculates the MD5 digest of the given InputStream object.
            // It will generate a 32 characters hex string.
            String digest = DigestUtils.md5Hex(is);
            System.out.println("Digest = " + digest);
            System.out.println("Length = " + digest.length());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The first thing we need to do is to add the import statement to our class to import the org.apache.commons.codec.digest.DigestUtils class. The DigestUtils.md5Hex() method define as a static method so that we don’t have to create an instance of DigestUtils class before we can use it. Before we create the InputStream object we define the path to our data file. I am creating the code snippet using Apache Maven as the build tools. That’s why you see my path the the data file is under the target/classes directory, because that directory is the default location where Maven will place the compiled classes and resource file. Next we create the File object from the defined path followed by creating the InputStream object of the File.

To generate the digest we can simply pass the instance of the InputStream object into the DigestUtils.md5Hex() method. And if there is no error occurred during the process we will get a 32 character of hex string as the output. One last thing that you have to do is the catch the possible exception thrown by the method. So we add the try-catch block and print the error stack trace to help us identify any error.

And here is the an example output generated by the code snippet above:

Digest = bafb2ec738d6de85170073c80a1008ad
Length = 32

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 calculates 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 use to verify the message.

package org.kodejava.example.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 = 3434955eecba50de487890d493ebaddd
Length      = 32

How do I decode a Base64 encoded binary?

package org.kodejava.example.commons.codec;

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Decode {
    public static void main(String[] args) {
        String hello = "SGVsbG8gV29ybGQ=";

        //
        // Decode a previously encoded string using decodeBase64 method and
        // passing the byte[] of the encoded string.
        //
        byte[] decoded = Base64.decodeBase64(hello.getBytes());

        //
        // Print the decoded array
        //
        System.out.println(Arrays.toString(decoded));

        //
        // Convert the decoded byte[] back to the original string and print
        // the result.
        //
        String decodedString = new String(decoded);
        System.out.println(hello + " = " + decodedString);
    }
}

The result of our code is:

[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
SGVsbG8gV29ybGQ= = Hello World

How do I encode binary data as Base64 string?

package org.kodejava.example.commons.codec;

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Encode {
    public static void main(String[] args) {
        String hello = "Hello World";

        //
        // The encodeBase64 method take a byte[] as the paramater. The byte[] 
        // can be from a simple string like in this example or it can be from
        // an image file data.
        //
        byte[] encoded = Base64.encodeBase64(hello.getBytes());

        //
        // Print the encoded byte array
        //
        System.out.println(Arrays.toString(encoded));

        //
        // Print the encoded string
        //
        String encodedString = new String(encoded);
        System.out.println(hello + " = " + encodedString);
    }
}

The result of our program:

[83, 71, 86, 115, 98, 71, 56, 103, 86, 50, 57, 121, 98, 71, 81, 61]
Hello World = SGVsbG8gV29ybGQ=