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.nio.charset.StandardCharsets;
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;
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( StandardCharsets.UTF_8 ) );
102
103 return getDigest( "SHA1", fis );
104 }
105 catch ( NoSuchAlgorithmException e )
106 {
107
108 return null;
109 }
110 }
111
112
113
114
115
116
117
118 public static String getSha1Digest( InputStream is )
119 {
120 try
121 {
122 return getDigest( "SHA1", is );
123 }
124 catch ( NoSuchAlgorithmException e )
125 {
126
127 return null;
128 }
129 }
130
131
132
133
134
135
136
137 public static String getSha1Digest( File file )
138 throws IOException
139 {
140 try ( FileInputStream fis = new FileInputStream( file ) )
141 {
142 return getDigest( "SHA1", fis );
143 }
144 catch ( NoSuchAlgorithmException | FileNotFoundException e )
145 {
146
147 return null;
148 }
149 }
150
151
152
153
154
155
156
157
158
159 public static String getMd5Digest( String content )
160 {
161 try
162 {
163 InputStream fis = new ByteArrayInputStream( content.getBytes( StandardCharsets.UTF_8 ) );
164
165 return getDigest( "MD5", fis );
166 }
167 catch ( NoSuchAlgorithmException e )
168 {
169
170 return null;
171 }
172 }
173
174
175
176
177
178
179
180 public static String getMd5Digest( InputStream is )
181 {
182 try
183 {
184 return getDigest( "MD5", is );
185 }
186 catch ( NoSuchAlgorithmException e )
187 {
188
189 return null;
190 }
191 }
192
193
194
195
196
197
198
199 public static String getMd5Digest( File file )
200 throws IOException
201 {
202
203 try ( InputStream fis = new FileInputStream( file ) )
204 {
205 return getDigest( "MD5", fis );
206 }
207 catch ( NoSuchAlgorithmException | FileNotFoundException e )
208 {
209
210 return null;
211 }
212 }
213
214
215
216 private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
217 'f' };
218
219
220
221
222
223
224
225 public static char[] encodeHex( byte[] data )
226 {
227 int l = data.length;
228
229 char[] out = new char[l << 1];
230
231
232 for ( int i = 0, j = 0; i < l; i++ )
233 {
234 out[j++] = DIGITS[( 0xF0 & data[i] ) >>> 4];
235 out[j++] = DIGITS[0x0F & data[i]];
236 }
237
238 return out;
239 }
240
241 }