Here you can find the source of computeMD5(InputStream inputStream)
Parameter | Description |
---|---|
inputStream | Stream to read |
Parameter | Description |
---|---|
NoSuchAlgorithmException | If system not support MD5 |
IOException | On reading stream issue |
public static String computeMD5(InputStream inputStream) throws NoSuchAlgorithmException, IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /**/*from ww w . j a v a2s . c o m*/ * Compute MD5 for a stream * * @param inputStream * Stream to read * @return MD5 of the stream * @throws NoSuchAlgorithmException * If system not support MD5 * @throws IOException * On reading stream issue */ public static String computeMD5(InputStream inputStream) throws NoSuchAlgorithmException, IOException { final MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] temp = new byte[4096]; int read = inputStream.read(temp); while (read >= 0) { md5.update(temp, 0, read); read = inputStream.read(temp); } inputStream.close(); inputStream = null; temp = md5.digest(); final StringBuffer stringBuffer = new StringBuffer(); for (final byte b : temp) { read = b & 0xFF; stringBuffer.append(Integer.toHexString((read >> 4) & 0xF)); stringBuffer.append(Integer.toHexString(read & 0xF)); } temp = null; return stringBuffer.toString(); } }