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