| 1 |
package net.oni2.aeinstaller.backend; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileInputStream; |
| 5 |
import java.io.FileNotFoundException; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.InputStream; |
| 8 |
import java.security.DigestInputStream; |
| 9 |
import java.security.MessageDigest; |
| 10 |
import java.security.NoSuchAlgorithmException; |
| 11 |
|
| 12 |
/** |
| 13 |
* @author Christian Illy |
| 14 |
*/ |
| 15 |
public class FileChecksum { |
| 16 |
|
| 17 |
/** |
| 18 |
* Calculate MD5 checksum of file |
| 19 |
* |
| 20 |
* @param f |
| 21 |
* File to calc checksum on |
| 22 |
* @return Checksum |
| 23 |
*/ |
| 24 |
public static byte[] calculateFileMD5(File f) { |
| 25 |
MessageDigest md = null; |
| 26 |
InputStream is = null; |
| 27 |
InputStream dis = null; |
| 28 |
byte[] md5 = null; |
| 29 |
try { |
| 30 |
md = MessageDigest.getInstance("MD5"); |
| 31 |
is = new FileInputStream(f); |
| 32 |
dis = new DigestInputStream(is, md); |
| 33 |
byte data[] = new byte[1024]; |
| 34 |
while (dis.read(data) > 0) { |
| 35 |
} |
| 36 |
md5 = md.digest(); |
| 37 |
} catch (NoSuchAlgorithmException e) { |
| 38 |
e.printStackTrace(); |
| 39 |
} catch (FileNotFoundException e) { |
| 40 |
e.printStackTrace(); |
| 41 |
} catch (IOException e) { |
| 42 |
e.printStackTrace(); |
| 43 |
} finally { |
| 44 |
if (dis != null) { |
| 45 |
try { |
| 46 |
dis.close(); |
| 47 |
} catch (IOException e) { |
| 48 |
e.printStackTrace(); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
return md5; |
| 53 |
} |
| 54 |
|
| 55 |
} |