import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5 工具类.
*
*/
public class MD5Util {
/**
* 获取该输入流的MD5值
*
* @param is
* @return
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static String getMD5(InputStream is) throws NoSuchAlgorithmException, IOException {
StringBuffer md5 = new StringBuffer();
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = is.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
};
byte[] mdBytes = md.digest();
// convert the byte to hex format
for (int i = 0; i < mdBytes.length; i++) {
md5.append(Integer.toString((mdBytes[i] & 0xff) + 0x100, 16).substring(1));
}
return md5.toString();
}
}
MD5Util
最新推荐文章于 2023-04-09 21:51:44 发布