View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.digest;
19  
20  import java.security.MessageDigest;
21  import java.security.NoSuchAlgorithmException;
22  
23  import org.apache.commons.codec.binary.Hex;
24  
25  /**
26   * Operations to simplifiy common {@link java.security.MessageDigest} tasks. This class is thread safe.
27   * 
28   * @author Apache Software Foundation
29   * @version $Id: DigestUtils.java 480406 2006-11-29 04:56:58Z bayard $
30   */
31  public class DigestUtils {
32  
33      /**
34       * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
35       * 
36       * @param algorithm
37       *            the name of the algorithm requested. See <a
38       *            href="http://java.sun.com/j2se/1.3/docs/guide/security/CryptoSpec.html#AppA">Appendix A in the Java
39       *            Cryptography Architecture API Specification & Reference</a> for information about standard algorithm
40       *            names.
41       * @return An MD5 digest instance.
42       * @see MessageDigest#getInstance(String)
43       * @throws RuntimeException
44       *             when a {@link java.security.NoSuchAlgorithmException} is caught.
45       */
46      static MessageDigest getDigest(String algorithm) {
47          try {
48              return MessageDigest.getInstance(algorithm);
49          } catch (NoSuchAlgorithmException e) {
50              throw new RuntimeException(e.getMessage());
51          }
52      }
53  
54      /**
55       * Returns an MD5 MessageDigest.
56       * 
57       * @return An MD5 digest instance.
58       * @throws RuntimeException
59       *             when a {@link java.security.NoSuchAlgorithmException} is caught.
60       */
61      private static MessageDigest getMd5Digest() {
62          return getDigest("MD5");
63      }
64  
65      /**
66       * Returns an SHA-256 digest.
67       * <p>
68       * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
69       * </p>
70       * 
71       * @return An SHA-256 digest instance.
72       * @throws RuntimeException
73       *             when a {@link java.security.NoSuchAlgorithmException} is caught.
74       */
75      private static MessageDigest getSha256Digest() {
76          return getDigest("SHA-256");
77      }
78  
79      /**
80       * Returns an SHA-384 digest.
81       * <p>
82       * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
83       * </p>
84       * 
85       * @return An SHA-384 digest instance.
86       * @throws RuntimeException
87       *             when a {@link java.security.NoSuchAlgorithmException} is caught.
88       */
89      private static MessageDigest getSha384Digest() {
90          return getDigest("SHA-384");
91      }
92  
93      /**
94       * Returns an SHA-512 digest.
95       * <p>
96       * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
97       * </p>
98       * 
99       * @return An SHA-512 digest instance.
100      * @throws RuntimeException
101      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
102      */
103     private static MessageDigest getSha512Digest() {
104         return getDigest("SHA-512");
105     }
106 
107     /**
108      * Returns an SHA-1 digest.
109      * 
110      * @return An SHA-1 digest instance.
111      * @throws RuntimeException
112      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
113      */
114     private static MessageDigest getShaDigest() {
115         return getDigest("SHA");
116     }
117 
118     /**
119      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
120      * 
121      * @param data
122      *            Data to digest
123      * @return MD5 digest
124      */
125     public static byte[] md5(byte[] data) {
126         return getMd5Digest().digest(data);
127     }
128 
129     /**
130      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
131      * 
132      * @param data
133      *            Data to digest
134      * @return MD5 digest
135      */
136     public static byte[] md5(String data) {
137         return md5(data.getBytes());
138     }
139 
140     /**
141      * Calculates the MD5 digest and returns the value as a 32 character hex string.
142      * 
143      * @param data
144      *            Data to digest
145      * @return MD5 digest as a hex string
146      */
147     public static String md5Hex(byte[] data) {
148         return new String(Hex.encodeHex(md5(data)));
149     }
150 
151     /**
152      * Calculates the MD5 digest and returns the value as a 32 character hex string.
153      * 
154      * @param data
155      *            Data to digest
156      * @return MD5 digest as a hex string
157      */
158     public static String md5Hex(String data) {
159         return new String(Hex.encodeHex(md5(data)));
160     }
161 
162     /**
163      * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
164      * 
165      * @param data
166      *            Data to digest
167      * @return SHA-1 digest
168      */
169     public static byte[] sha(byte[] data) {
170         return getShaDigest().digest(data);
171     }
172 
173     /**
174      * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
175      * 
176      * @param data
177      *            Data to digest
178      * @return SHA-1 digest
179      */
180     public static byte[] sha(String data) {
181         return sha(data.getBytes());
182     }
183 
184     /**
185      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
186      * <p>
187      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
188      * </p>
189      * 
190      * @param data
191      *            Data to digest
192      * @return SHA-256 digest
193      */
194     public static byte[] sha256(byte[] data) {
195         return getSha256Digest().digest(data);
196     }
197 
198     /**
199      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
200      * <p>
201      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
202      * </p>
203      * 
204      * @param data
205      *            Data to digest
206      * @return SHA-256 digest
207      */
208     public static byte[] sha256(String data) {
209         return sha256(data.getBytes());
210     }
211 
212     /**
213      * Calculates the SHA-256 digest and returns the value as a hex string.
214      * <p>
215      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
216      * </p>
217      * 
218      * @param data
219      *            Data to digest
220      * @return SHA-256 digest as a hex string
221      */
222     public static String sha256Hex(byte[] data) {
223         return new String(Hex.encodeHex(sha256(data)));
224     }
225 
226     /**
227      * Calculates the SHA-256 digest and returns the value as a hex string.
228      * <p>
229      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
230      * </p>
231      * 
232      * @param data
233      *            Data to digest
234      * @return SHA-256 digest as a hex string
235      */
236     public static String sha256Hex(String data) {
237         return new String(Hex.encodeHex(sha256(data)));
238     }
239 
240     /**
241      * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
242      * <p>
243      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
244      * </p>
245      * 
246      * @param data
247      *            Data to digest
248      * @return SHA-384 digest
249      */
250     public static byte[] sha384(byte[] data) {
251         // FIXME: check Sun docs for how to get a sha 384 digest
252         return getSha384Digest().digest(data);
253     }
254 
255     /**
256      * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
257      * <p>
258      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
259      * </p>
260      * 
261      * @param data
262      *            Data to digest
263      * @return SHA-384 digest
264      */
265     public static byte[] sha384(String data) {
266         return sha384(data.getBytes());
267     }
268 
269     /**
270      * Calculates the SHA-384 digest and returns the value as a hex string.
271      * <p>
272      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
273      * </p>
274      * 
275      * @param data
276      *            Data to digest
277      * @return SHA-384 digest as a hex string
278      */
279     public static String sha384Hex(byte[] data) {
280         return new String(Hex.encodeHex(sha384(data)));
281     }
282 
283     /**
284      * Calculates the SHA-384 digest and returns the value as a hex string.
285      * <p>
286      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
287      * </p>
288      * 
289      * @param data
290      *            Data to digest
291      * @return SHA-384 digest as a hex string
292      */
293     public static String sha384Hex(String data) {
294         return new String(Hex.encodeHex(sha384(data)));
295     }
296 
297     /**
298      * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
299      * <p>
300      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
301      * </p>
302      * 
303      * @param data
304      *            Data to digest
305      * @return SHA-512 digest
306      */
307     public static byte[] sha512(byte[] data) {
308         return getSha512Digest().digest(data);
309     }
310 
311     /**
312      * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
313      * <p>
314      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
315      * </p>
316      * 
317      * @param data
318      *            Data to digest
319      * @return SHA-512 digest
320      */
321     public static byte[] sha512(String data) {
322         return sha512(data.getBytes());
323     }
324 
325     /**
326      * Calculates the SHA-512 digest and returns the value as a hex string.
327      * <p>
328      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
329      * </p>
330      * 
331      * @param data
332      *            Data to digest
333      * @return SHA-512 digest as a hex string
334      */
335     public static String sha512Hex(byte[] data) {
336         return new String(Hex.encodeHex(sha512(data)));
337     }
338 
339     /**
340      * Calculates the SHA-512 digest and returns the value as a hex string.
341      * <p>
342      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
343      * </p>
344      * 
345      * @param data
346      *            Data to digest
347      * @return SHA-512 digest as a hex string
348      */
349     public static String sha512Hex(String data) {
350         return new String(Hex.encodeHex(sha512(data)));
351     }
352 
353     /**
354      * Calculates the SHA-1 digest and returns the value as a hex string.
355      * 
356      * @param data
357      *            Data to digest
358      * @return SHA-1 digest as a hex string
359      */
360     public static String shaHex(byte[] data) {
361         return new String(Hex.encodeHex(sha(data)));
362     }
363 
364     /**
365      * Calculates the SHA-1 digest and returns the value as a hex string.
366      * 
367      * @param data
368      *            Data to digest
369      * @return SHA-1 digest as a hex string
370      */
371     public static String shaHex(String data) {
372         return new String(Hex.encodeHex(sha(data)));
373     }
374 }