1- 获取 MD5 校验和java.security.DigestInputStream
(请参阅Getting a File's MD5 Checksum in Java)
MessageDigest md = MessageDigest.getInstance("MD5");
InputStream is = new FileInputStream("file.txt");
try {
is = new DigestInputStream(is, md);
// read stream to EOF as normal...
}
finally {
is.close();
}
byte[] digest = md.digest();
2-比较两个字节[]的小方法
boolean safeArrayEquals(byte[] a1, byte[] a2) {
if (a1 == null || a2 == null) {
return (a1 == a2);
}
if (a1.length != a2.length) {
return false;
}
byte result = 0;
for (int i = 0; i < a1.length; i++) {
result |= a1[i] ^ a2[i];
}
return (result == 0);
}