In the How do I calculate the MD5 digest of a string? 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
Maven Dependencies
<!-- https://search.maven.org/remotecontent?filepath=commons-codec/commons-codec/1.12/commons-codec-1.12.jar -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
Would be nice to show a maven dependency to know where exactly
org.apache.commons.codec.digest.DigestUtils
resides.@petrosipov:disqus
The dependency could be:
The
InputStream
needs to be closed infinally { }
block (or usetry (resource) { }
syntax )