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 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 String result = null;
53
54 try
55 {
56 try
57 {
58 byte[] buffer = new byte[1024];
59
60 MessageDigest md = MessageDigest.getInstance( alg );
61
62 int numRead;
63
64 do
65 {
66 numRead = is.read( buffer );
67
68 if ( numRead > 0 )
69 {
70 md.update( buffer, 0, numRead );
71 }
72 }
73 while ( numRead != -1 );
74
75 result = new String( encodeHex( md.digest() ) );
76 }
77 finally
78 {
79 is.close();
80 }
81 }
82 catch ( IOException e )
83 {
84
85 result = null;
86 }
87
88 return result;
89 }
90
91
92
93
94
95
96
97
98
99 public static String getSha1Digest( String content )
100 {
101 try
102 {
103 InputStream fis = new ByteArrayInputStream( content.getBytes( "UTF-8" ) );
104
105 return getDigest( "SHA1", fis );
106 }
107 catch ( NoSuchAlgorithmException e )
108 {
109
110 return null;
111 }
112 catch ( UnsupportedEncodingException e )
113 {
114
115 return null;
116 }
117 }
118
119
120
121
122
123
124
125 public static String getSha1Digest( InputStream is )
126 {
127 try
128 {
129 return getDigest( "SHA1", is );
130 }
131 catch ( NoSuchAlgorithmException e )
132 {
133
134 return null;
135 }
136 }
137
138
139
140
141
142
143
144 public static String getSha1Digest( File file )
145 {
146 FileInputStream fis = null;
147
148 try
149 {
150 fis = new FileInputStream( file );
151
152 return getDigest( "SHA1", fis );
153 }
154 catch ( NoSuchAlgorithmException e )
155 {
156
157 return null;
158 }
159 catch ( FileNotFoundException e )
160 {
161
162 return null;
163 }
164 finally
165 {
166 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 InputStream fis = new ByteArrayInputStream( content.getBytes( "UTF-8" ) );
183
184 return getDigest( "MD5", fis );
185 }
186 catch ( NoSuchAlgorithmException e )
187 {
188
189 return null;
190 }
191 catch ( UnsupportedEncodingException e )
192 {
193
194 return null;
195 }
196 }
197
198
199
200
201
202
203
204 public static String getMd5Digest( InputStream is )
205 {
206 try
207 {
208 return getDigest( "MD5", is );
209 }
210 catch ( NoSuchAlgorithmException e )
211 {
212
213 return null;
214 }
215 }
216
217
218
219
220
221
222
223 public static String getMd5Digest( File file )
224 {
225 FileInputStream fis = null;
226
227 try
228 {
229 fis = new FileInputStream( file );
230
231 return getDigest( "MD5", fis );
232 }
233 catch ( NoSuchAlgorithmException e )
234 {
235
236 return null;
237 }
238 catch ( FileNotFoundException e )
239 {
240
241 return null;
242 }
243 finally
244 {
245 IOUtil.close( fis );
246 }
247 }
248
249
250
251 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 int l = data.length;
263
264 char[] out = new char[l << 1];
265
266
267 for ( int i = 0, j = 0; i < l; i++ )
268 {
269 out[j++] = DIGITS[( 0xF0 & data[i] ) >>> 4];
270 out[j++] = DIGITS[0x0F & data[i]];
271 }
272
273 return out;
274 }
275
276 }