source: java/installer2/src/net/oni2/aeinstaller/backend/FileChecksum.java@ 853

Last change on this file since 853 was 804, checked in by alloc, 12 years ago

AEI2.02:

File size: 1.2 KB
RevLine 
[804]1package net.oni2.aeinstaller.backend;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
8import java.security.DigestInputStream;
9import java.security.MessageDigest;
10import java.security.NoSuchAlgorithmException;
11
12/**
13 * @author Christian Illy
14 */
15public 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}
Note: See TracBrowser for help on using the repository browser.