Tuesday, 25 August 2015

Calculating MD5 and SHA values in Java

MD5, SHA are well known algorithms to calculate message digest. These are also known as hash functions, takes arbitrary length of data as input and generates fixed length hash value. Hash function are mainly used to check data integrity in cryptography.

In this post, you are going to learn, how to generate messages digest for a string, file using SHA, MD5 algorithms.
Following snippet is used to calculate message digest for a string.

private static String getMessageDigest(String message, String algorithm) {
 MessageDigest digest;
 try {
  digest = MessageDigest.getInstance(algorithm);
  byte data[] = digest.digest(message.getBytes("UTF-8"));
  return convertByteArrayToHexString(data);
 } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return null;
}

First we need to get MessageDigest instance by calling getInstance method, 'getInstance' method takes algorithm name as argument to generate digest.


We can calculate message digest for a file, by converting file data to a string and calling above function.

private static String getMessageDigest(Path file, String algorithm) {
 String message;
 try {
  message = new String(Files.readAllBytes(file));
  return getMessageDigest(message, algorithm);
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return null;

}


Following is the complete working application.

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

public class FileUtilities {

 private static String convertByteArrayToHexString(byte[] arrayBytes) {
  return DatatypeConverter.printHexBinary(arrayBytes);
 }

 private static String getMessageDigest(String message, String algorithm) {
  MessageDigest digest;
  try {
   digest = MessageDigest.getInstance(algorithm);
   byte data[] = digest.digest(message.getBytes("UTF-8"));
   return convertByteArrayToHexString(data);

  } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }

 private static String getMessageDigest(Path file, String algorithm) {
  String message;
  try {
   message = new String(Files.readAllBytes(file));
   return getMessageDigest(message, algorithm);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;

 }

 public static String generateMD5(Path file) {
  return getMessageDigest(file, "MD5");
 }

 public static String generateSHA1(Path file) {
  return getMessageDigest(file, "SHA-1");
 }

 public static String generateSHA256(Path file) {
  return getMessageDigest(file, "SHA-256");
 }

 public static String generateMD5(String message) {
  return getMessageDigest(message, "MD5");
 }

 public static String generateSHA1(String message) {
  return getMessageDigest(message, "SHA-1");
 }

 public static String generateSHA256(String message) {
  return getMessageDigest(message, "SHA-256");
 }

 public static void main(String args[]) {
  String data = "Hello";

  System.out.println(generateSHA1(data));
  System.out.println(generateMD5(data));
  System.out.println(generateSHA256(data));

 }
}


Output
F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0
8B1A9953C4611296A827ABF8C47804D7
185F8DB32271FE25F561A6FC938B2E264306EC304EDA518007D1764826381969



No comments:

Post a Comment