1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public static String getChecksum(FileInputStream from) throws ChecksumException, IOException {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
int bufferSize = 64000;
byte[] bufferFile = new byte[bufferSize];
for (int i = 0; ; i++) {
int len = from.read(bufferFile);
md.update(bufferFile, 0, len);
if (len < 0)
break;
}
return (encodeMD5(md.digest()));
//return dig;
} catch (NoSuchAlgorithmException ns) {
throw new ChecksumException("Algorithm is not implemented");
}
}
public static String encodeMD5(byte[] binaryData) {
if (binaryData.length != 16) {
return "";
}
final char[] hexadecimal = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char[] buffer = new char[32];
for (int i = 0; i < 16; i++) {
int low = (int) (binaryData[i] & 0x0f);
int high = (int) ((binaryData[i] & 0xf0) >> 4);
buffer[i * 2] = hexadecimal[high];
buffer[i * 2 + 1] = hexadecimal[low];
}
return new String(buffer);
} |
Partager