Android MD5 Encode md5File(File file)

Here you can find the source of md5File(File file)

Description

md File

License

Apache License

Declaration

public static String md5File(File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;

public class Main {
    public static String md5File(File file) {
        if (!file.isFile()) {
            return null;
        }//w w w.  ja  v  a  2 s.  co m
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }

        } catch (Exception e) {
            return null;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    //do nothing
                }
            }
        }
        return new String(Hex.encodeHex(digest.digest()));
    }
}

Related

  1. md5(final String s)
  2. md5(final String s)
  3. md5(final String s)
  4. md5Byte(String in)
  5. md5Byte(byte[] in)
  6. md5Hex(String str)
  7. computeHashMD5(final String text)
  8. getMD5String(File file)
  9. getMD5String(String str)