Java MD5 File computeMD5Sum(final File file)

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

Description

Compute MD5 sum of a file.

License

LGPL

Parameter

Parameter Description
file the input file

Exception

Parameter Description
IOException In case of an I/O problem or digest error

Return

a string with the MD5 sum

Declaration

public static String computeMD5Sum(final File file) throws IOException 

Method Source Code

//package com.java2s;
/*//from   www  . ja v  a2 s.c o m
 *                  Eoulsan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public License version 2.1 or
 * later and CeCILL-C. This should be distributed with the code.
 * If you do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/lgpl-2.1.txt
 *      http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'?cole normale sup?rieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Eoulsan project and its aims,
 * or to join the Eoulsan Google group, visit the home page
 * at:
 *
 *      http://outils.genomique.biologie.ens.fr/eoulsan
 *
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.math.BigInteger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /** The default size of the buffer. */
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * Compute MD5 sum of a file.
     * @param file the input file
     * @return a string with the MD5 sum
     * @throws IOException In case of an I/O problem or digest error
     */
    public static String computeMD5Sum(final File file) throws IOException {

        if (file == null) {
            throw new NullPointerException("The file argument is null");
        }

        return computeMD5Sum(new FileInputStream(file));
    }

    /**
     * Compute MD5 sum of a file.
     * @param input the InputStream to read from
     * @return a string with the MD5 sum
     * @throws IOException In case of an I/O problem or digest error
     */
    public static String computeMD5Sum(final InputStream input) throws IOException {

        final MessageDigest md5Digest;
        try {
            md5Digest = MessageDigest.getInstance("MD5");

            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

            int n = 0;

            while (-1 != (n = input.read(buffer))) {
                md5Digest.update(buffer, 0, n);
            }

        } catch (NoSuchAlgorithmException e) {
            throw new IOException("No MD5 digest algorithm found: " + e.getMessage());
        }

        input.close();

        final BigInteger bigInt = new BigInteger(1, md5Digest.digest());

        return bigInt.toString(16);
    }
}

Related

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