Here you can find the source of md5sum(InputStream in)
public static String md5sum(InputStream in) throws IOException, NoSuchAlgorithmException
//package com.java2s; /*/* w ww . j a v a 2 s . c o m*/ * Copyright (C) 2012 Joseph Areeda <joseph.areeda at ligo.org> * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { public static String md5sum(InputStream in) throws IOException, NoSuchAlgorithmException { in.reset(); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] dataBytes = new byte[1024]; int nread; while ((nread = in.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } return getMd5String(md); } public static String md5sum(byte[] data) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(data, 0, data.length); return getMd5String(md); } public static String md5sum(float[] data) { String ret = "error"; try { byte[] b = float2ByteArray(data); ret = md5sum(b); } catch (Exception ex) { String ermsg = "Calculating md5sum of data: " + ex.getClass().getSimpleName() + " - " + ex.getLocalizedMessage(); } return ret; } public static String getMd5String(MessageDigest md) { byte[] mdbytes = md.digest(); //convert the byte to hex format method 1 StringBuilder sb = new StringBuilder(); for (int i = 0; i < mdbytes.length; i++) { sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } /** * * @param data * @return */ public static byte[] float2ByteArray(float[] data) throws IOException { byte[] ret; int len = data.length; ByteArrayOutputStream bos = new ByteArrayOutputStream(len * Float.SIZE / 8); DataOutputStream dos = new DataOutputStream(bos); for (int in = 0; in < len; in++) { dos.writeFloat(data[in]); } ret = bos.toByteArray(); return ret; } }