package me.ele.amigo.utils; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class DigestUtils { private static final int STREAM_BUFFER_LENGTH = 1024 * 8; private static MessageDigest getDigest(String algorithm) { try { return MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } } private static MessageDigest getMd5Digest() { return getDigest("MD5"); } private static byte[] digest(MessageDigest digest, InputStream data) throws IOException { byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); while (read > -1) { digest.update(buffer, 0, read); read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); } return digest.digest(); } public static byte[] md5(InputStream data) throws IOException { return digest(getMd5Digest(), data); } }