1 | |
package org.apache.maven.index.packer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.ByteArrayInputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.FileNotFoundException; |
26 | |
import java.io.IOException; |
27 | |
import java.io.InputStream; |
28 | |
import java.io.UnsupportedEncodingException; |
29 | |
import java.security.MessageDigest; |
30 | |
import java.security.NoSuchAlgorithmException; |
31 | |
|
32 | |
import org.codehaus.plexus.util.IOUtil; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class DigesterUtils |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private static String getDigest( String alg, InputStream is ) |
50 | |
throws NoSuchAlgorithmException |
51 | |
{ |
52 | 0 | String result = null; |
53 | |
|
54 | |
try |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | byte[] buffer = new byte[1024]; |
59 | |
|
60 | 0 | MessageDigest md = MessageDigest.getInstance( alg ); |
61 | |
|
62 | |
int numRead; |
63 | |
|
64 | |
do |
65 | |
{ |
66 | 0 | numRead = is.read( buffer ); |
67 | |
|
68 | 0 | if ( numRead > 0 ) |
69 | |
{ |
70 | 0 | md.update( buffer, 0, numRead ); |
71 | |
} |
72 | |
} |
73 | 0 | while ( numRead != -1 ); |
74 | |
|
75 | 0 | result = new String( encodeHex( md.digest() ) ); |
76 | |
} |
77 | |
finally |
78 | |
{ |
79 | 0 | is.close(); |
80 | 0 | } |
81 | |
} |
82 | 0 | catch ( IOException e ) |
83 | |
{ |
84 | |
|
85 | 0 | result = null; |
86 | 0 | } |
87 | |
|
88 | 0 | return result; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public static String getSha1Digest( String content ) |
100 | |
{ |
101 | |
try |
102 | |
{ |
103 | 0 | InputStream fis = new ByteArrayInputStream( content.getBytes( "UTF-8" ) ); |
104 | |
|
105 | 0 | return getDigest( "SHA1", fis ); |
106 | |
} |
107 | 0 | catch ( NoSuchAlgorithmException e ) |
108 | |
{ |
109 | |
|
110 | 0 | return null; |
111 | |
} |
112 | 0 | catch ( UnsupportedEncodingException e ) |
113 | |
{ |
114 | |
|
115 | 0 | return null; |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static String getSha1Digest( InputStream is ) |
126 | |
{ |
127 | |
try |
128 | |
{ |
129 | 0 | return getDigest( "SHA1", is ); |
130 | |
} |
131 | 0 | catch ( NoSuchAlgorithmException e ) |
132 | |
{ |
133 | |
|
134 | 0 | return null; |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public static String getSha1Digest( File file ) |
145 | |
{ |
146 | 0 | FileInputStream fis = null; |
147 | |
|
148 | |
try |
149 | |
{ |
150 | 0 | fis = new FileInputStream( file ); |
151 | |
|
152 | 0 | return getDigest( "SHA1", fis ); |
153 | |
} |
154 | 0 | catch ( NoSuchAlgorithmException e ) |
155 | |
{ |
156 | |
|
157 | 0 | return null; |
158 | |
} |
159 | 0 | catch ( FileNotFoundException e ) |
160 | |
{ |
161 | |
|
162 | 0 | return null; |
163 | |
} |
164 | |
finally |
165 | |
{ |
166 | 0 | IOUtil.close( fis ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public static String getMd5Digest( String content ) |
179 | |
{ |
180 | |
try |
181 | |
{ |
182 | 0 | InputStream fis = new ByteArrayInputStream( content.getBytes( "UTF-8" ) ); |
183 | |
|
184 | 0 | return getDigest( "MD5", fis ); |
185 | |
} |
186 | 0 | catch ( NoSuchAlgorithmException e ) |
187 | |
{ |
188 | |
|
189 | 0 | return null; |
190 | |
} |
191 | 0 | catch ( UnsupportedEncodingException e ) |
192 | |
{ |
193 | |
|
194 | 0 | return null; |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public static String getMd5Digest( InputStream is ) |
205 | |
{ |
206 | |
try |
207 | |
{ |
208 | 0 | return getDigest( "MD5", is ); |
209 | |
} |
210 | 0 | catch ( NoSuchAlgorithmException e ) |
211 | |
{ |
212 | |
|
213 | 0 | return null; |
214 | |
} |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public static String getMd5Digest( File file ) |
224 | |
{ |
225 | 0 | FileInputStream fis = null; |
226 | |
|
227 | |
try |
228 | |
{ |
229 | 0 | fis = new FileInputStream( file ); |
230 | |
|
231 | 0 | return getDigest( "MD5", fis ); |
232 | |
} |
233 | 0 | catch ( NoSuchAlgorithmException e ) |
234 | |
{ |
235 | |
|
236 | 0 | return null; |
237 | |
} |
238 | 0 | catch ( FileNotFoundException e ) |
239 | |
{ |
240 | |
|
241 | 0 | return null; |
242 | |
} |
243 | |
finally |
244 | |
{ |
245 | 0 | IOUtil.close( fis ); |
246 | |
} |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | 0 | private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', |
252 | |
'f' }; |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public static char[] encodeHex( byte[] data ) |
261 | |
{ |
262 | 0 | int l = data.length; |
263 | |
|
264 | 0 | char[] out = new char[l << 1]; |
265 | |
|
266 | |
|
267 | 0 | for ( int i = 0, j = 0; i < l; i++ ) |
268 | |
{ |
269 | 0 | out[j++] = DIGITS[( 0xF0 & data[i] ) >>> 4]; |
270 | 0 | out[j++] = DIGITS[0x0F & data[i]]; |
271 | |
} |
272 | |
|
273 | 0 | return out; |
274 | |
} |
275 | |
|
276 | |
} |