1 package org.apache.maven.index.packer;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * A util class to calculate various digests on Strings. Useful for some simple password management.
36 *
37 * @author cstamas
38 */
39 public class DigesterUtils
40 {
41 /**
42 * Calculates a digest for a String user the requested algorithm.
43 *
44 * @param alg
45 * @param content
46 * @return
47 * @throws NoSuchAlgorithmException
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 // hrm
85 result = null;
86 }
87
88 return result;
89 }
90
91 // SHA1
92
93 /**
94 * Calculates a SHA1 digest for a string.
95 *
96 * @param content
97 * @return
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 // will not happen
110 return null;
111 }
112 catch ( UnsupportedEncodingException e )
113 {
114 // will not happen
115 return null;
116 }
117 }
118
119 /**
120 * Calculates a SHA1 digest for a stream.
121 *
122 * @param content
123 * @return
124 */
125 public static String getSha1Digest( InputStream is )
126 {
127 try
128 {
129 return getDigest( "SHA1", is );
130 }
131 catch ( NoSuchAlgorithmException e )
132 {
133 // will not happen
134 return null;
135 }
136 }
137
138 /**
139 * Calculates a SHA1 digest for a file.
140 *
141 * @param content
142 * @return
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 // will not happen
157 return null;
158 }
159 catch ( FileNotFoundException e )
160 {
161 // will not happen
162 return null;
163 }
164 finally
165 {
166 IOUtil.close( fis );
167 }
168 }
169
170 // MD5
171
172 /**
173 * Calculates a SHA1 digest for a string.
174 *
175 * @param content
176 * @return
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 // will not happen
189 return null;
190 }
191 catch ( UnsupportedEncodingException e )
192 {
193 // will not happen
194 return null;
195 }
196 }
197
198 /**
199 * Calculates a SHA1 digest for a stream.
200 *
201 * @param content
202 * @return
203 */
204 public static String getMd5Digest( InputStream is )
205 {
206 try
207 {
208 return getDigest( "MD5", is );
209 }
210 catch ( NoSuchAlgorithmException e )
211 {
212 // will not happen
213 return null;
214 }
215 }
216
217 /**
218 * Calculates a SHA1 digest for a file.
219 *
220 * @param content
221 * @return
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 // will not happen
236 return null;
237 }
238 catch ( FileNotFoundException e )
239 {
240 // will not happen
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 * Blatantly copied from commons-codec version 1.3
256 *
257 * @param data
258 * @return
259 */
260 public static char[] encodeHex( byte[] data )
261 {
262 int l = data.length;
263
264 char[] out = new char[l << 1];
265
266 // two characters form the hex value.
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 }