Skip to content

Instantly share code, notes, and snippets.

@toomasr
Created July 23, 2018 07:56
Show Gist options
  • Save toomasr/9c37e5312b453e0a99d5c3fcf283719b to your computer and use it in GitHub Desktop.
Save toomasr/9c37e5312b453e0a99d5c3fcf283719b to your computer and use it in GitHub Desktop.
Packing the same bytes twice
import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import org.zeroturnaround.zip.ZipUtil;
public class ZipUtilPackMd5 {
public static void main(String[] args) throws Exception {
String fileName = "target/classes/ZipUtilPackMd5.class";
File file = new File(fileName);
if (!file.exists())
throw new RuntimeException(fileName + " does not exist");
byte[] bytes = Files.readAllBytes(file.toPath());
System.out.println("Input file " + md5(bytes));
byte[] zippedBytes = ZipUtil.packEntry(file);
System.out.println(md5(zippedBytes));
zippedBytes = ZipUtil.packEntry(file);
System.out.println(md5(zippedBytes));
}
private static String md5(byte[] bytes) throws Exception {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(bytes, 0, bytes.length);
return "md5: " + new BigInteger(1, m.digest()).toString(16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment