KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > digest > DigestUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.digest;
18
19 import java.security.MessageDigest JavaDoc;
20 import java.security.NoSuchAlgorithmException JavaDoc;
21
22 import org.apache.commons.codec.binary.Hex;
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 JavaDoc getDigest(String JavaDoc algorithm) {
40         try {
41             return MessageDigest.getInstance(algorithm);
42         } catch (NoSuchAlgorithmException JavaDoc e) {
43             throw new RuntimeException JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc md5Hex(byte[] data) {
97         return new String JavaDoc(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 JavaDoc md5Hex(String JavaDoc data) {
108         return new String JavaDoc(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 JavaDoc 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 JavaDoc shaHex(byte[] data) {
140         return new String JavaDoc(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 JavaDoc shaHex(String JavaDoc data) {
150         return new String JavaDoc(Hex.encodeHex(sha(data)));
151     }
152
153 }
154
Popular Tags