Thursday 21 May 2015

Message Digest functions

Message digest is a digital summary for information. Message digest functions (like SHA, MD5) are used to calculate message digest for given information. 

As shown in above figure, data/document is given as input to Message Digest function, Message Digest function performs mathematical operations on the document and generate message digest for given document. Usually Message digests are 128 bits to 160 bits in length. If two documents are identical, then they has same message digest. If two documents are not identical, then they have different message digests.

Message digest is used to provide data integrity. Suppose ‘A’ want to send a document to ‘B’.

Step 1: A computes Message digest for the document.

Step 2: A sends document along with message digest.

Step 3: Once ‘B’ receives the document, ‘B’ re computes message digest, and compares new message digest with original message digest. If the two message digests do not match, then 'B' knows the message was altered or corrupted.

Java Support to calculate Message Digest
java.security.MessageDigest class provides support for message digest.

How to calculate Message Digest
It is 3-step process.

Step 1: Get the instance of MessageDigest class. MessageDigest class provides static factory methods, to get instance of MessageDigest.

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
public static MessageDigest getInstance(String algorithm,String provider) throws NoSuchAlgorithmException, NoSuchProviderException
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException

Parameters “algorithm” specifies the name of the algorithm to be used, “provider” represents the provider for the security, a provider implements java security api.

Java API supports following algorithms
            a. MD5
            b. SHA-1
            c. SHA-256

Step 2:  Feed the digest with data. MessageDigest API provides following methods to feed digest.
            public void update(byte input)
            public void update(byte[] input, int offset, int len)
            public void update(byte[] input)
            public final void update(ByteBuffer input)

Step 3: Compute the digest. MessageDigest class provides following methods, to calculate message digest.
public byte[] digest()
public int digest(byte[] buf, int offset, int len) throws DigestException
public byte[] digest(byte[] input)
The digest is reset after this call is made.

Application to compute message digest for a file

Following application computes message digest for a file.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.io.*;

public class MessageDigestUtil {

 /**
  * Computes message digest for given file
  * 
  * @param fileName
  *            file to calculate digest
  * @param algorithm
  *            Algorithm to use
  * @return message digest for given file
  */
 public static byte[] getMessageDigest(String fileName, String algorithm) {

  try {
   MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

   /* Read file and feed the data to messageDigest */
   File fileToDigest = new File(fileName);
   byte input[] = readFileAsBytes(fileToDigest);

   messageDigest.update(input);
   return messageDigest.digest();

  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }

  return new byte[0];
 }

 /**
  * Convert file data to byte array
  * 
  * @param file
  * @return byte array
  */
 public static byte[] readFileAsBytes(File file) {

  ByteArrayOutputStream byteArrayStream = null;
  InputStream inoutStream = null;

  try {
   byte[] buffer = new byte[4096];
   byteArrayStream = new ByteArrayOutputStream();
   inoutStream = new FileInputStream(file);
   int read = 0;
   while ((read = inoutStream.read(buffer)) != -1) {
    byteArrayStream.write(buffer, 0, read);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (byteArrayStream != null)
     byteArrayStream.close();

    if (inoutStream != null)
     inoutStream.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return byteArrayStream.toByteArray();
 }

}

public class MessageDigestUtilTest {
 public static void main(String args[]) {
  byte digest[] = MessageDigestUtil.getMessageDigest("glob.txt", "MD5");

  for (byte b : digest) {
   System.out.print(b + " ");
  }
 }
}


Output
102 -13 -33 -20 102 71 -128 89 96 -65 116 115 -46 -107 -96 42

No comments:

Post a Comment