Java MD5 File computeMD5(File file)

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

Description

compute MD

License

Open Source License

Declaration

public static String computeMD5(File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w w w.  jav  a 2 s.c o  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String computeMD5(File file) {
        if (file == null || file.isDirectory() || !file.exists())
            return null;
        MessageDigest md5Checker;
        try {
            md5Checker = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file));
            int read = -1;
            while ((read = fis.read()) != -1) {
                md5Checker.update((byte) read);
            }
            byte[] digest = md5Checker.digest();
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < digest.length; i++) {
                if ((digest[i] & 0xFF) < 0x10)
                    buf.append('0');
                buf.append(Integer.toHexString(digest[i] & 0xFF));
            }
            return buf.toString();
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException e) {
            return null;
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    // ignore
                }
        }
    }
}

Related

  1. computeMD5(File file)
  2. computeMD5(final File file)
  3. computeMD5(String filename)
  4. computeMd5Digest(final File file)
  5. computeMD5Sum(final File file)