Android File MD5 Hash getMD5Sum(String file)

Here you can find the source of getMD5Sum(String file)

Description

Returns the lowercase string representation of the file's MD5 sum.

Declaration

public static String getMD5Sum(String file) throws IOException 

Method Source Code

//package com.java2s;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**/*from   w  w w.j ava  2  s  . co  m*/
     * Returns the lowercase string representation of the file's MD5 sum.
     */
    public static String getMD5Sum(String file) throws IOException {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            InputStream is = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int read = 0;
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            is.close();
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            return bigInt.toString(16);
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
    }
}