1 package org.eclipse.aether.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.Collection;
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32
33
34
35
36 public final class ChecksumUtils
37 {
38
39 private ChecksumUtils()
40 {
41
42 }
43
44
45
46
47
48
49
50
51 public static String read( File checksumFile )
52 throws IOException
53 {
54 String checksum = "";
55
56 FileInputStream fis = new FileInputStream( checksumFile );
57 try
58 {
59 BufferedReader br = new BufferedReader( new InputStreamReader( fis, "UTF-8" ), 512 );
60 try
61 {
62 while ( true )
63 {
64 String line = br.readLine();
65 if ( line == null )
66 {
67 break;
68 }
69 line = line.trim();
70 if ( line.length() > 0 )
71 {
72 checksum = line;
73 break;
74 }
75 }
76 }
77 finally
78 {
79 try
80 {
81 br.close();
82 }
83 catch ( IOException e )
84 {
85
86 }
87 }
88 }
89 finally
90 {
91 try
92 {
93 fis.close();
94 }
95 catch ( IOException e )
96 {
97
98 }
99 }
100
101 if ( checksum.matches( ".+= [0-9A-Fa-f]+" ) )
102 {
103 int lastSpacePos = checksum.lastIndexOf( ' ' );
104 checksum = checksum.substring( lastSpacePos + 1 );
105 }
106 else
107 {
108 int spacePos = checksum.indexOf( ' ' );
109
110 if ( spacePos != -1 )
111 {
112 checksum = checksum.substring( 0, spacePos );
113 }
114 }
115
116 return checksum;
117 }
118
119
120
121
122
123
124
125
126
127
128
129 public static Map<String, Object> calc( File dataFile, Collection<String> algos )
130 throws IOException
131 {
132 Map<String, Object> results = new LinkedHashMap<String, Object>();
133
134 Map<String, MessageDigest> digests = new LinkedHashMap<String, MessageDigest>();
135 for ( String algo : algos )
136 {
137 try
138 {
139 digests.put( algo, MessageDigest.getInstance( algo ) );
140 }
141 catch ( NoSuchAlgorithmException e )
142 {
143 results.put( algo, e );
144 }
145 }
146
147 FileInputStream fis = new FileInputStream( dataFile );
148 try
149 {
150 for ( byte[] buffer = new byte[32 * 1024];; )
151 {
152 int read = fis.read( buffer );
153 if ( read < 0 )
154 {
155 break;
156 }
157 for ( MessageDigest digest : digests.values() )
158 {
159 digest.update( buffer, 0, read );
160 }
161 }
162 }
163 finally
164 {
165 try
166 {
167 fis.close();
168 }
169 catch ( IOException e )
170 {
171
172 }
173 }
174
175 for ( Map.Entry<String, MessageDigest> entry : digests.entrySet() )
176 {
177 byte[] bytes = entry.getValue().digest();
178
179 results.put( entry.getKey(), toHexString( bytes ) );
180 }
181
182 return results;
183 }
184
185
186
187
188
189
190
191
192 public static String toHexString( byte[] bytes )
193 {
194 if ( bytes == null )
195 {
196 return null;
197 }
198
199 StringBuilder buffer = new StringBuilder( bytes.length * 2 );
200
201 for ( byte aByte : bytes )
202 {
203 int b = aByte & 0xFF;
204 if ( b < 0x10 )
205 {
206 buffer.append( '0' );
207 }
208 buffer.append( Integer.toHexString( b ) );
209 }
210
211 return buffer.toString();
212 }
213
214 }