001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.util;
025:
026: import java.security.MessageDigest;
027: import java.security.NoSuchAlgorithmException;
028:
029: /**
030: * Utility class that simplifies the generation of MD5 fingerprints.
031: */
032: public final class HashUtils {
033:
034: private static final String MD5 = "MD5";
035:
036: private static final String SHA1 = "SHA-1";
037:
038: private static MessageDigest md5Digest;
039:
040: private static MessageDigest sha1Digest;
041:
042: private HashUtils() {
043: }
044:
045: /**
046: * Hashes a String using the MD5 algorithm and returns the result as a
047: * String of hexadecimal numbers. This method is synchronized to avoid
048: * excessive MessageDigest object creation. If calling this method becomes a
049: * bottleneck in your code, you may wish to maintain a pool of MessageDigest
050: * objects instead of using this method.
051: *
052: * @param data the String to compute the hash of.
053: * @return a hashed version of the passed-in String
054: */
055: public static synchronized String md5(String data) {
056: if (md5Digest == null) {
057: try {
058: md5Digest = MessageDigest.getInstance(MD5);
059: } catch (NoSuchAlgorithmException e) {
060: throw new RuntimeException(e);
061: }
062: }
063: md5Digest.update(data.getBytes());
064: return toHex(md5Digest.digest());
065: }
066:
067: /**
068: * Hashes a String using the SHA-1 algorithm and returns the result as a
069: * String of hexadecimal numbers. This method is synchronized to avoid
070: * excessive MessageDigest object creation. If calling this method becomes a
071: * bottleneck in your code, you may wish to maintain a pool of MessageDigest
072: * objects instead of using this method.
073: *
074: * @param data the String to compute the hash of.
075: * @return a hashed version of the passed-in String
076: */
077: public static synchronized String sha1(String data) {
078: if (sha1Digest == null) {
079: try {
080: sha1Digest = MessageDigest.getInstance(SHA1);
081: } catch (NoSuchAlgorithmException e) {
082: throw new RuntimeException(e);
083: }
084: }
085: sha1Digest.update(data.getBytes());
086: return toHex(sha1Digest.digest());
087: }
088:
089: /**
090: * Converts an array of bytes into a String representing each byte as an
091: * unsigned hex number.
092: *
093: * @param buffer array of bytes to convert
094: * @return generated hex string
095: */
096: public static String toHex(byte[] buffer) {
097: StringBuffer sb = new StringBuffer();
098: String s = null;
099: for (int i = 0; i < buffer.length; i++) {
100: s = Integer.toHexString(buffer[i] & 0xff);
101: if (s.length() < 2) {
102: sb.append('0');
103: }
104: sb.append(s);
105: }
106: return sb.toString();
107: }
108:
109: }
|