View Javadoc
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.nio.charset.StandardCharsets;
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;
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( StandardCharsets.UTF_8 ) );
102 
103             return getDigest( "SHA1", fis );
104         }
105         catch ( NoSuchAlgorithmException e )
106         {
107             // will not happen
108             return null;
109         }
110     }
111 
112     /**
113      * Calculates a SHA1 digest for a stream.
114      * 
115      * @param is
116      * @return
117      */
118     public static String getSha1Digest( InputStream is )
119     {
120         try
121         {
122             return getDigest( "SHA1", is );
123         }
124         catch ( NoSuchAlgorithmException e )
125         {
126             // will not happen
127             return null;
128         }
129     }
130 
131     /**
132      * Calculates a SHA1 digest for a file.
133      * 
134      * @param file
135      * @return
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             // will not happen
147             return null;
148         }
149     }
150 
151     // MD5
152 
153     /**
154      * Calculates a SHA1 digest for a string.
155      * 
156      * @param content
157      * @return
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             // will not happen
170             return null;
171         }
172     }
173 
174     /**
175      * Calculates a SHA1 digest for a stream.
176      * 
177      * @param is
178      * @return
179      */
180     public static String getMd5Digest( InputStream is )
181     {
182         try
183         {
184             return getDigest( "MD5", is );
185         }
186         catch ( NoSuchAlgorithmException e )
187         {
188             // will not happen
189             return null;
190         }
191     }
192 
193     /**
194      * Calculates a SHA1 digest for a file.
195      * 
196      * @param file
197      * @return
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             // will not happen
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      * Blatantly copied from commons-codec version 1.3
221      * 
222      * @param data
223      * @return
224      */
225     public static char[] encodeHex( byte[] data )
226     {
227         int l = data.length;
228 
229         char[] out = new char[l << 1];
230 
231         // two characters form the hex value.
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 }