Java MD5 File computeMD5(final File file)

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

Description

Returns MD5 for specified file.

License

Open Source License

Parameter

Parameter Description
file file to process

Return

MD5

Declaration

public static String computeMD5(final File file) 

Method Source Code

//package com.java2s;
/*/*from   www.  j  a v  a2 s.c  om*/
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.math.BigInteger;

import java.security.MessageDigest;

public class Main {
    /**
     * Buffer size for MD5 calculations.
     */
    private static final int MD5_BUFFER_LENGTH = 102400;

    /**
     * Returns MD5 for specified file.
     *
     * @param file file to process
     * @return MD5
     */
    public static String computeMD5(final File file) {
        return computeMD5(file, MD5_BUFFER_LENGTH);
    }

    /**
     * Returns MD5 for specified file and using a buffer of specified length.
     *
     * @param file         file to process
     * @param bufferLength buffer length
     * @return MD5
     */
    public static String computeMD5(final File file, final int bufferLength) {
        try {
            return computeMD5(new FileInputStream(file), bufferLength);
        } catch (final FileNotFoundException e) {
            return null;
        }
    }

    /**
     * Returns MD5 using the specified data stream.
     *
     * @param is data stream to process
     * @return MD5
     */
    public static String computeMD5(final InputStream is) {
        return computeMD5(is, MD5_BUFFER_LENGTH);
    }

    /**
     * Returns MD5 using the specified data stream and a buffer of specified length.
     *
     * @param is           data stream to process
     * @param bufferLength buffer length
     * @return MD5
     */
    public static String computeMD5(final InputStream is, final int bufferLength) {
        final BufferedInputStream bis = new BufferedInputStream(is);
        try {
            final MessageDigest digest = MessageDigest.getInstance("MD5");
            final byte[] buffer = new byte[bufferLength];
            int bytesRead;
            while ((bytesRead = bis.read(buffer, 0, buffer.length)) > 0) {
                digest.update(buffer, 0, bytesRead);
            }
            final byte[] md5sum = digest.digest();
            final BigInteger bigInt = new BigInteger(1, md5sum);
            return bigInt.toString(16);
        } catch (final Throwable e) {
            return null;
        } finally {
            try {
                bis.close();
            } catch (final Throwable e) {
                //
            }
        }
    }
}

Related

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