KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > util > HashUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.util;
25
26 import java.security.MessageDigest JavaDoc;
27 import java.security.NoSuchAlgorithmException JavaDoc;
28
29 /**
30  * Utility class that simplifies the generation of MD5 fingerprints.
31  */

32 public final class HashUtils {
33
34     private static final String JavaDoc MD5 = "MD5";
35     
36     private static MessageDigest JavaDoc md5Digest;
37     
38     private HashUtils() {
39     }
40     
41     /**
42      * Hashes a String using the Md5 algorithm and returns the result as a
43      * String of hexadecimal numbers. This method is synchronized to avoid
44      * excessive MessageDigest object creation. If calling this method becomes a
45      * bottleneck in your code, you may wish to maintain a pool of MessageDigest
46      * objects instead of using this method.
47      *
48      * @param data the String to compute the hash of.
49      * @return a hashed version of the passed-in String
50      */

51     public static synchronized String JavaDoc md5(String JavaDoc data) {
52         if (md5Digest == null) {
53             try {
54                 md5Digest = MessageDigest.getInstance(MD5);
55             }
56             catch (NoSuchAlgorithmException JavaDoc e) {
57                 throw new RuntimeException JavaDoc(e);
58             }
59         }
60         md5Digest.update(data.getBytes());
61         return toHex(md5Digest.digest());
62     }
63     
64     /**
65      * Converts an array of bytes into a String representing each byte as an
66      * unsigned hex number.
67      *
68      * @param buffer array of bytes to convert
69      * @return generated hex string
70      */

71     public static String JavaDoc toHex(byte[] buffer) {
72         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
73         String JavaDoc s = null;
74         for (int i = 0; i < buffer.length; i++) {
75             s = Integer.toHexString(buffer[i] & 0xff);
76             if (s.length() < 2) {
77                 sb.append('0');
78             }
79             sb.append(s);
80         }
81         return sb.toString();
82     }
83
84 }
85
Popular Tags