001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.digest;
018:
019: import java.security.MessageDigest;
020: import java.security.NoSuchAlgorithmException;
021:
022: import org.apache.commons.codec.binary.Hex;
023:
024: /**
025: * Operations to simplifiy common {@link java.security.MessageDigest} tasks. This
026: * class is thread safe.
027: *
028: * @author Apache Software Foundation
029: */
030: public class DigestUtils {
031:
032: /**
033: * Returns a MessageDigest for the given <code>algorithm</code>.
034: *
035: * @param algorithm The MessageDigest algorithm name.
036: * @return An MD5 digest instance.
037: * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
038: */
039: static MessageDigest getDigest(String algorithm) {
040: try {
041: return MessageDigest.getInstance(algorithm);
042: } catch (NoSuchAlgorithmException e) {
043: throw new RuntimeException(e.getMessage());
044: }
045: }
046:
047: /**
048: * Returns an MD5 MessageDigest.
049: *
050: * @return An MD5 digest instance.
051: * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
052: */
053: private static MessageDigest getMd5Digest() {
054: return getDigest("MD5");
055: }
056:
057: /**
058: * Returns an SHA digest.
059: *
060: * @return An SHA digest instance.
061: * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
062: */
063: private static MessageDigest getShaDigest() {
064: return getDigest("SHA");
065: }
066:
067: /**
068: * Calculates the MD5 digest and returns the value as a 16 element
069: * <code>byte[]</code>.
070: *
071: * @param data Data to digest
072: * @return MD5 digest
073: */
074: public static byte[] md5(byte[] data) {
075: return getMd5Digest().digest(data);
076: }
077:
078: /**
079: * Calculates the MD5 digest and returns the value as a 16 element
080: * <code>byte[]</code>.
081: *
082: * @param data Data to digest
083: * @return MD5 digest
084: */
085: public static byte[] md5(String data) {
086: return md5(data.getBytes());
087: }
088:
089: /**
090: * Calculates the MD5 digest and returns the value as a 32 character
091: * hex string.
092: *
093: * @param data Data to digest
094: * @return MD5 digest as a hex string
095: */
096: public static String md5Hex(byte[] data) {
097: return new String(Hex.encodeHex(md5(data)));
098: }
099:
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: }
|