1 package net.jpountz.util;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.security.MessageDigest;
9 import java.security.NoSuchAlgorithmException;
12 * @author Tuukka Lehtonen
14 public final class ChecksumUtil {
16 private static byte[] computeSum(InputStream in) throws IOException {
18 throw new IllegalArgumentException("Input cannot be null!");
22 md = MessageDigest.getInstance("MD5");
23 } catch (NoSuchAlgorithmException e) {
24 throw new Error("MD5 digest must be supported by JVM");
26 byte[] data = new byte[64 * 1024];
29 int read = in.read(data);
33 md.update(data, 0, read);
37 public static byte[] computeSum(Path p) throws IOException {
38 try (InputStream in = Files.newInputStream(p)) {
39 return computeSum(in);
43 public static byte[] computeSum(URL url) throws IOException {
44 try (InputStream in = url.openStream()) {
45 return computeSum(in);