// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.security.*;

public class FileHasher {
   public static String hash(String filename) throws IOException, NoSuchAlgorithmException {
      byte[] b = new byte[(int) (new File(filename)).length()];
      InputStream is = new FileInputStream(filename);
      is.read(b);
      is.close();
      MessageDigest md;
      md = MessageDigest.getInstance("MD5");
      return toHex(md.digest(b));
   }
   private static String toHex(byte[] ba) {
      char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < ba.length; i++) {
         sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
         sb.append(hexdigit[ba[i] & 0x0F]);
      }
      return sb.toString();
   }
   public static void main(String[] args) throws Exception {
      System.out.println(hash("C:\\x1.cpp"));
      System.out.println(hash("C:\\x2.cpp"));
   }
}
