Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to get MD5 checksum for any given file in Java? Use commons-codec’s DigestUtils.md5Hex

$
0
0

Create MD5 checksum in Java

Tighten the security of your enterprise application is the biggest challenge of any organization.

Consider this actual production scenario:

  1. You have an application which reads the value from a file
  2. Based on the file value it performs some operations like add/delete/execute
  3. You have deployed this application in production environment
  4. What if some unauthorized person changes the value of that file without your knowledge?
  5. Your application simply gets new value from a file and runs the logic which may cause unexpected outcome
  6. If you may have MD5 checksum enabled for that file – you could have created an exception with clear error message and you could have prevented disaster or unexpected outcome

What is MD5 checksum?

The MD5 checksum for a file is a 128-bit value, something like a fingerprint of the file. It can be useful both for comparing the files and their integrity control.

To get the same value for different file is nearly impossible. In this tutorial we will create simple Java program which creates and returns MD5 values for a given file. In our case it’s index.php file.

Let’s get started:

Step-1

Create public class CrunchifyGetMD5ForFile.java

Step-2

Import below two maven dependencies to your project’s pom.xml file. Tutorial to convert project to maven project.

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.10</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>

We are using commons-codec and commons-io libraries.

Step-3

Create MD5 checksum using DigestUtils.md5Hex utility and print result on console.

Create MD5 checksum using DigestUtils.md5Hex utility

Here is a full program

package com.crunchify.tutorials;

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

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

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyGetMD5ForFile {

	public static String crunchifyGetMd5ForFile(String crunchifyFile) {
		File myFile = new File(crunchifyFile);
		return crunchifyGetMd5ForFile(myFile);
	}

	public static String crunchifyGetMd5ForFile(File crunchifyFile) {
		String crunchifyValue = null;
		FileInputStream crunchifyInputStream = null;
		try {
			crunchifyInputStream = new FileInputStream(crunchifyFile);

			// md5Hex converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
			// The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
			crunchifyValue = DigestUtils.md5Hex(IOUtils.toByteArray(crunchifyInputStream));
		} catch (IOException e) {
			log("Hey there is an error: " + e);
		} finally {
			IOUtils.closeQuietly(crunchifyInputStream);
		}
		return crunchifyValue;
	}

	// Simple log util
	private static void log(String string) {
		System.out.println(string);

	}

	public static void main(String agrs[]) {
		// Let's get MD5 for File index.php located at /Users/<username>/Downloads
		String file = "/Users/<username>/Downloads/index.php";
		String md5Value = crunchifyGetMd5ForFile(file);
		log("File: " + file + " \nValue: " + md5Value);
	}
}

Output:

File: /Users/<username>/Downloads/index.php 
Value: 8ab0ecbe3570a2ee5f59e26a92528c5e

How are you going to use this MD5 checksum at runtime to verify file integrity?

You could compare this MD5 checksum at runtime with value stored in y0ur database, like, MySQL, Oracle, etc. There are number of other ways too you could achieve the same but we will discuss this in future tutorials.

Have anything to add to this article? Please chime in and join the conversion.

The post How to get MD5 checksum for any given file in Java? Use commons-codec’s DigestUtils.md5Hex appeared first on Crunchify.
Author: App Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles