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.id;
19  
20  import java.security.MessageDigest;
21  import java.security.NoSuchAlgorithmException;
22  
23  
24  /**
25   * Operations to simplifiy common {@link java.security.MessageDigest} tasks.  This
26   * class is thread safe.
27   *
28   * @author Apache Software Foundation
29   */
30  public class DigestUtils {
31  
32      /**
33       * Returns a MessageDigest for the given <code>algorithm</code>.
34       *
35       * @param algorithm The MessageDigest algorithm name.
36       * @return An MD5 digest instance.
37       * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
38       */
39      static MessageDigest getDigest(String algorithm) {
40          try {
41              return MessageDigest.getInstance(algorithm);
42          } catch (NoSuchAlgorithmException e) {
43              throw new RuntimeException(e.getMessage());
44          }
45      }
46  
47      /**
48       * Returns an MD5 MessageDigest.
49       *
50       * @return An MD5 digest instance.
51       * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
52       */
53      private static MessageDigest getMd5Digest() {
54          return getDigest("MD5");
55      }
56  
57      /**
58       * Returns an SHA digest.
59       *
60       * @return An SHA digest instance.
61       * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
62       */
63      private static MessageDigest getShaDigest() {
64          return getDigest("SHA");
65      }
66  
67      /**
68       * Calculates the MD5 digest and returns the value as a 16 element 
69       * <code>byte[]</code>.
70       *
71       * @param data Data to digest
72       * @return MD5 digest
73       */
74      public static byte[] md5(byte[] data) {
75          return getMd5Digest().digest(data);
76      }
77  
78      /**
79       * Calculates the MD5 digest and returns the value as a 16 element 
80       * <code>byte[]</code>.
81       *
82       * @param data Data to digest
83       * @return MD5 digest
84       */
85      public static byte[] md5(String data) {
86          return md5(data.getBytes());
87      }
88  
89      /**
90       * Calculates the MD5 digest and returns the value as a 32 character 
91       * hex string.
92       *
93       * @param data Data to digest
94       * @return MD5 digest as a hex string
95       */
96      public static String md5Hex(byte[] data) {
97          return new String(Hex.encodeHex(md5(data)));
98      }
99  
100     /**
101      * Calculates the MD5 digest and returns the value as a 32 character 
102      * hex string.
103      *
104      * @param data Data to digest
105      * @return MD5 digest as a hex string
106      */
107     public static String md5Hex(String data) {
108         return new String(Hex.encodeHex(md5(data)));
109     }
110 
111     /**
112      * Calculates the SHA digest and returns the value as a 
113      * <code>byte[]</code>.
114      *
115      * @param data Data to digest
116      * @return SHA digest
117      */
118     public static byte[] sha(byte[] data) {
119         return getShaDigest().digest(data);
120     }
121 
122     /**
123      * Calculates the SHA digest and returns the value as a 
124      * <code>byte[]</code>.
125      *
126      * @param data Data to digest
127      * @return SHA digest
128      */
129     public static byte[] sha(String data) {
130         return sha(data.getBytes());
131     }
132 
133     /**
134      * Calculates the SHA digest and returns the value as a hex string.
135      *
136      * @param data Data to digest
137      * @return SHA digest as a hex string
138      */
139     public static String shaHex(byte[] data) {
140         return new String(Hex.encodeHex(sha(data)));
141     }
142 
143     /**
144      * Calculates the SHA digest and returns the value as a hex string.
145      *
146      * @param data Data to digest
147      * @return SHA digest as a hex string
148      */
149     public static String shaHex(String data) {
150         return new String(Hex.encodeHex(sha(data)));
151     }
152 
153 }