Java MD5 String md5(final File file)

Here you can find the source of md5(final File file)

Description

md

License

Open Source License

Declaration

public static String md5(final File file) 

Method Source Code

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

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

import java.io.InputStream;

import java.security.MessageDigest;

public class Main {
    public static String md5(final File file) {
        try {/*  w w  w  . ja  va  2  s.c  om*/
            final InputStream fin = new FileInputStream(file);
            final MessageDigest md5 = MessageDigest.getInstance("MD5");

            // read file into buffer and update digest
            byte[] buffer = new byte[1024];
            int read;
            while ((read = fin.read(buffer)) > 0) {
                md5.update(buffer, 0, read);
            }
            fin.close();

            // format output
            byte[] digest = md5.digest();
            if (digest == null)
                return null;
            String hash = "";
            for (int i = 0; i < digest.length; i++) {
                hash += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
            }

            return hash;
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. md5(File f)
  2. md5(File file)
  3. md5(File file)
  4. md5(File file)
  5. md5(File gcdZipFile)
  6. md5(final InputStream in)
  7. md5(final String data)
  8. md5(final String inData)
  9. md5(final String input)