View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.packer;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.nio.charset.StandardCharsets;
28  import java.security.MessageDigest;
29  import java.security.NoSuchAlgorithmException;
30  
31  /**
32   * A util class to calculate various digests on Strings. Useful for some simple password management.
33   *
34   * @author cstamas
35   */
36  public class DigesterUtils {
37      /**
38       * Calculates a digest for a String user the requested algorithm.
39       *
40       * @param alg
41       * @param is
42       * @return
43       * @throws NoSuchAlgorithmException
44       */
45      private static String getDigest(String alg, InputStream is) throws NoSuchAlgorithmException {
46          String result;
47  
48          try {
49              try {
50                  byte[] buffer = new byte[1024];
51  
52                  MessageDigest md = MessageDigest.getInstance(alg);
53  
54                  int numRead;
55  
56                  do {
57                      numRead = is.read(buffer);
58  
59                      if (numRead > 0) {
60                          md.update(buffer, 0, numRead);
61                      }
62                  } while (numRead != -1);
63  
64                  result = new String(encodeHex(md.digest()));
65              } finally {
66                  is.close();
67              }
68          } catch (IOException e) {
69              // hrm
70              result = null;
71          }
72  
73          return result;
74      }
75  
76      // SHA1
77  
78      /**
79       * Calculates a SHA1 digest for a string.
80       *
81       * @param content
82       * @return
83       */
84      public static String getSha1Digest(String content) {
85          try {
86              InputStream fis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
87  
88              return getDigest("SHA1", fis);
89          } catch (NoSuchAlgorithmException e) {
90              // will not happen
91              return null;
92          }
93      }
94  
95      /**
96       * Calculates a SHA1 digest for a stream.
97       *
98       * @param is
99       * @return
100      */
101     public static String getSha1Digest(InputStream is) {
102         try {
103             return getDigest("SHA1", is);
104         } catch (NoSuchAlgorithmException e) {
105             // will not happen
106             return null;
107         }
108     }
109 
110     /**
111      * Calculates a SHA1 digest for a file.
112      *
113      * @param file
114      * @return
115      */
116     public static String getSha1Digest(File file) throws IOException {
117         try (FileInputStream fis = new FileInputStream(file)) {
118             return getDigest("SHA1", fis);
119         } catch (NoSuchAlgorithmException | FileNotFoundException e) {
120             // will not happen
121             return null;
122         }
123     }
124 
125     // MD5
126 
127     /**
128      * Calculates a SHA1 digest for a string.
129      *
130      * @param content
131      * @return
132      */
133     public static String getMd5Digest(String content) {
134         try {
135             InputStream fis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
136 
137             return getDigest("MD5", fis);
138         } catch (NoSuchAlgorithmException e) {
139             // will not happen
140             return null;
141         }
142     }
143 
144     /**
145      * Calculates a SHA1 digest for a stream.
146      *
147      * @param is
148      * @return
149      */
150     public static String getMd5Digest(InputStream is) {
151         try {
152             return getDigest("MD5", is);
153         } catch (NoSuchAlgorithmException e) {
154             // will not happen
155             return null;
156         }
157     }
158 
159     /**
160      * Calculates a SHA1 digest for a file.
161      *
162      * @param file
163      * @return
164      */
165     public static String getMd5Digest(File file) throws IOException {
166 
167         try (InputStream fis = new FileInputStream(file)) {
168             return getDigest("MD5", fis);
169         } catch (NoSuchAlgorithmException | FileNotFoundException e) {
170             // will not happen
171             return null;
172         }
173     }
174 
175     // --
176 
177     private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
178     };
179 
180     /**
181      * Blatantly copied from commons-codec version 1.3
182      *
183      * @param data
184      * @return
185      */
186     public static char[] encodeHex(byte[] data) {
187         int l = data.length;
188 
189         char[] out = new char[l << 1];
190 
191         // two characters form the hex value.
192         for (int i = 0, j = 0; i < l; i++) {
193             out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
194             out[j++] = DIGITS[0x0F & data[i]];
195         }
196 
197         return out;
198     }
199 }