Java MD5 String md5File(File f)

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

Description

md File

License

Open Source License

Declaration

public static String md5File(File f) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.security.DigestInputStream;
import java.security.MessageDigest;

public class Main {
    public static String md5File(File f) {
        MessageDigest md = null;/*from  www . ja v  a 2s.  com*/
        InputStream is = null;
        try {
            md = MessageDigest.getInstance("MD5");
            is = new FileInputStream(f);

            is = new DigestInputStream(is, md);
            // read stream to EOF as normal...
            byte[] buffer = new byte[4096];//allocate a small array.
            while (is.read(buffer) != -1) {
            } //read file in chunks

        } catch (Throwable ignore) {
            //don't care.
            ignore.printStackTrace();
            return null;

        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
        }
        if (null != md) {
            return toHexString(md.digest());
        }
        return null;
    }

    public static String toHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toHexString(0xff & bytes[i]));
        }
        return sb.toString();
    }
}

Related

  1. md5EncriptionPassword(String str)
  2. md5encrypt(String phrase)
  3. md5Encrypt(String str)
  4. md5Encrypt(String str)
  5. md5Encryption(String str)
  6. md5file(File file)
  7. md5FromFile(File file)
  8. md5fromFile(String path)
  9. md5Hex(String data)