1 package org.simantics.fastlz;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
8 import java.security.MessageDigest;
9 import java.security.NoSuchAlgorithmException;
12 * @author Tuukka Lehtonen
14 public final class ChecksumUtil {
16 public static byte[] computeSum(byte[] in) {
18 throw new IllegalArgumentException("Input cannot be null!");
20 MessageDigest md = MessageDigest.getInstance("MD5");
21 md.update(in, 0, in.length);
23 } catch (NoSuchAlgorithmException e) {
24 throw new Error("MD5 digest must be supported by JVM");
28 public static byte[] computeSum(InputStream in) throws IOException {
30 throw new IllegalArgumentException("Input cannot be null!");
34 md = MessageDigest.getInstance("MD5");
35 } catch (NoSuchAlgorithmException e) {
36 throw new Error("MD5 digest must be supported by JVM");
38 byte[] data = new byte[64 * 1024];
41 int read = in.read(data);
45 md.update(data, 0, read);
49 public static byte[] computeSum(File f) throws IOException {
50 InputStream in = null;
52 in = new FileInputStream(f);
53 return computeSum(in);
60 public static byte[] computeSum(URL url) throws IOException {
61 InputStream in = null;
63 in = url.openStream();
64 return computeSum(in);