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 /**
33 * A util class to calculate various digests on Strings. Useful for some simple password management.
34 *
35 * @author cstamas
36 */
37 public class DigesterUtils
38 {
39 /**
40 * Calculates a digest for a String user the requested algorithm.
41 *
42 * @param alg
43 * @param is
44 * @return
45 * @throws NoSuchAlgorithmException
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 // hrm
83 result = null;
84 }
85
86 return result;
87 }
88
89 // SHA1
90
91 /**
92 * Calculates a SHA1 digest for a string.
93 *
94 * @param content
95 * @return
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 // will not happen
108 return null;
109 }
110 catch ( UnsupportedEncodingException e )
111 {
112 // will not happen
113 return null;
114 }
115 }
116
117 /**
118 * Calculates a SHA1 digest for a stream.
119 *
120 * @param is
121 * @return
122 */
123 public static String getSha1Digest( InputStream is )
124 {
125 try
126 {
127 return getDigest( "SHA1", is );
128 }
129 catch ( NoSuchAlgorithmException e )
130 {
131 // will not happen
132 return null;
133 }
134 }
135
136 /**
137 * Calculates a SHA1 digest for a file.
138 *
139 * @param file
140 * @return
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 // will not happen
152 return null;
153 }
154 catch ( FileNotFoundException e )
155 {
156 // will not happen
157 return null;
158 }
159 }
160
161 // MD5
162
163 /**
164 * Calculates a SHA1 digest for a string.
165 *
166 * @param content
167 * @return
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 // will not happen
180 return null;
181 }
182 catch ( UnsupportedEncodingException e )
183 {
184 // will not happen
185 return null;
186 }
187 }
188
189 /**
190 * Calculates a SHA1 digest for a stream.
191 *
192 * @param is
193 * @return
194 */
195 public static String getMd5Digest( InputStream is )
196 {
197 try
198 {
199 return getDigest( "MD5", is );
200 }
201 catch ( NoSuchAlgorithmException e )
202 {
203 // will not happen
204 return null;
205 }
206 }
207
208 /**
209 * Calculates a SHA1 digest for a file.
210 *
211 * @param file
212 * @return
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 // will not happen
225 return null;
226 }
227 catch ( FileNotFoundException e )
228 {
229 // will not happen
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 * Blatantly copied from commons-codec version 1.3
241 *
242 * @param data
243 * @return
244 */
245 public static char[] encodeHex( byte[] data )
246 {
247 int l = data.length;
248
249 char[] out = new char[l << 1];
250
251 // two characters form the hex value.
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 }