KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > ChecksumHelper


1 package fr.jayasoft.ivy.util;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileReader JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.security.MessageDigest JavaDoc;
10 import java.security.NoSuchAlgorithmException JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 public class ChecksumHelper {
15     
16     private static Map JavaDoc _algorithms = new HashMap JavaDoc();
17     static {
18         _algorithms.put("md5", "MD5");
19         _algorithms.put("sha1", "SHA-1");
20     }
21     
22     public static boolean check(File JavaDoc dest, File JavaDoc checksumFile, String JavaDoc algorithm) throws IOException JavaDoc {
23         String JavaDoc csFileContent = FileUtil.readEntirely(new BufferedReader JavaDoc(new FileReader JavaDoc(checksumFile))).trim().toLowerCase();
24         String JavaDoc expected;
25         int spaceIndex = csFileContent.indexOf(' ');
26         if (spaceIndex != -1) {
27             expected = csFileContent.substring(0, spaceIndex);
28         } else {
29             expected = csFileContent;
30         }
31         
32         String JavaDoc computed = computeAsString(dest, algorithm).trim().toLowerCase();
33         return expected.equals(computed);
34     }
35     
36     public static String JavaDoc computeAsString(File JavaDoc f, String JavaDoc algorithm) throws IOException JavaDoc {
37         return byteArrayToHexString(compute(f, algorithm));
38     }
39     
40     private static byte[] compute(File JavaDoc f, String JavaDoc algorithm) throws IOException JavaDoc {
41         InputStream JavaDoc is = new FileInputStream JavaDoc(f);
42
43         try {
44             MessageDigest JavaDoc md = getMessageDigest(algorithm);
45             md.reset();
46
47             byte[] buf = new byte[2048];
48             int len = 0;
49             while ((len = is.read(buf)) != -1) {
50                 md.update(buf, 0, len);
51             }
52             return md.digest();
53         } finally {
54             is.close();
55         }
56     }
57
58
59     private static MessageDigest JavaDoc getMessageDigest(String JavaDoc algorithm) {
60         String JavaDoc mdAlgorithm = (String JavaDoc) _algorithms.get(algorithm);
61         if (mdAlgorithm == null) {
62             throw new IllegalArgumentException JavaDoc("unknown algorithm "+algorithm);
63         }
64         try {
65             return MessageDigest.getInstance(mdAlgorithm);
66         } catch (NoSuchAlgorithmException JavaDoc e) {
67             throw new IllegalArgumentException JavaDoc("unknown algorithm "+algorithm);
68         }
69     }
70
71
72     // byte to hex string converter
73
private final static char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
74     /**
75      * Convert a byte[] array to readable string format. This makes the "hex" readable!
76      * @return result String buffer in String format
77      * @param in byte[] buffer to convert to string format
78      */

79     public static String JavaDoc byteArrayToHexString(byte in[]) {
80         byte ch = 0x00;
81
82         if (in == null || in.length <= 0) {
83             return null;
84         }
85
86         StringBuffer JavaDoc out = new StringBuffer JavaDoc(in.length * 2);
87
88         for (int i = 0; i < in.length; i++) {
89             ch = (byte) (in[i] & 0xF0); // Strip off high nibble
90
ch = (byte) (ch >>> 4); // shift the bits down
91
ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
92

93             out.append(CHARS[ (int) ch]); // convert the nibble to a String Character
94
ch = (byte) (in[i] & 0x0F); // Strip off low nibble
95
out.append(CHARS[ (int) ch]); // convert the nibble to a String Character
96
}
97
98         return out.toString();
99     }
100
101 }
102
Popular Tags