If you need to calculate the MD5 of a large file , you can use this:
Import
import java.security.MessageDigest;
Method:
private byte[] calculateMD5ofFile(String location) throws IOException, NoSuchAlgorithmException { FileInputStream fs= new FileInputStream(location); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] buffer=new byte[bufferSize]; int bytes=0; do{ bytes=fs.read(buffer,0,bufferSize); if(bytes>0) md.update(buffer,0,bytes); }while(bytes>0); byte[] Md5Sum = md.digest(); return Md5Sum; }
Refrence: https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html
Convert byte array to Hex. use it
public static String ByteArraytoHexString(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); }
Refrence In Java, how do I convert an array of bytes to a string of hexadecimal digits while keeping leading zeros?
Mahdi Rafatjah Sep 16 '16 at 8:58 2016-09-16 08:58
source share