Here you can find the source of computeMD5(File file)
Parameter | Description |
---|---|
file | The File to compute its MD5 checksum. |
Parameter | Description |
---|---|
IOException | Occurred Exception. |
public static String computeMD5(File file) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany * Technical University Darmstadt, Germany * Chalmers University of Technology, Sweden * 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:/* ww w . j av a 2 s .c o m*/ * Technical University Darmstadt - initial API and implementation and/or initial documentation *******************************************************************************/ 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 { /** * Computes the MD5 checksum of the given {@link File}. * @param file The {@link File} to compute its MD5 checksum. * @return The computed MD5 checksum. * @throws IOException Occurred Exception. */ public static String computeMD5(File file) throws IOException { if (file == null) { throw new IOException("Can't compute MD5 without a File."); } if (!file.isFile()) { throw new IOException("Can't compute MD5, because \"" + file + "\" is not an existing file."); } return computeMD5(new FileInputStream(file)); } /** * Computes the MD5 checksum of the given {@link InputStream} and closes it. * @param in The {@link InputStream} which provides the content to compute its MD5 checksum. The {@link InputStream} will be closed. * @return The computed MD5 checksum. * @throws IOException Occurred Exception. */ public static String computeMD5(InputStream in) throws IOException { if (in == null) { throw new IOException("Can't compute MD5 without an InputStream."); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[8192]; int read = 0; while ((read = in.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); return bigInt.toString(16); } catch (NoSuchAlgorithmException e) { throw new IOException("Algorithm MD5 is not available."); } finally { in.close(); } } }