/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package edu.harvard.iq.dvn.core.web.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * * @author xyang */ public class MD5Checksum implements java.io.Serializable { public MD5Checksum() { } public synchronized String CalculateMD5 (String datafile) { MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } FileInputStream fis = null; try { fis = new FileInputStream(datafile); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } byte[] dataBytes = new byte[1024]; int nread; try { while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } } catch (IOException ex) { throw new RuntimeException(ex); } byte[] mdbytes = md.digest(); StringBuilder sb = new StringBuilder(""); for (int i = 0; i < mdbytes.length; i++) { sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } }