KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > MD5Digest


1 /*
2  * $Id: MD5Digest.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Alexey Pavlov <alexnet@users.sourceforge.net>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.util;
26
27 import java.security.MessageDigest JavaDoc;
28
29 import org.apache.log.Logger;
30 import org.jresearch.gossip.exception.SystemException;
31 import org.jresearch.gossip.log.avalon.JGossipLog;
32
33 /**
34  * Utility class MD5Digest. Calculates MD5 hash from input information.
35  *
36  * @author <a HREF="alexnet@sourceforge.net">A. Pavlov</a>
37  * @version $version$ $Date: 2005/06/07 12:32:27 $
38  */

39 public class MD5Digest {
40
41     /**
42      * Calculate MD5 hash from username and password combination.
43      *
44      * @param username
45      * String with username to digest
46      * @param password
47      * String with username to digest
48      * @return MD5 hash.
49      */

50     public static String JavaDoc digest(String JavaDoc username, String JavaDoc password) {
51         try {
52             MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
53             md.update(username.getBytes("UTF8"));
54             md.update(password.getBytes("UTF8"));
55             byte[] bytes = md.digest();
56             return byteArrayToHexString(bytes);
57         } catch (Exception JavaDoc e) {
58             try {
59                 Logger log = JGossipLog.getInstance().getAppLogger();
60                 if (log.isFatalErrorEnabled()) {
61                     log.fatalError("Can't calculate MD5 hash.", e);
62                 }
63             } catch (SystemException e1) { /* Ignore this exception. */
64             }
65             return null;
66         }
67     }
68
69     /**
70      * Convert a byte[] array to readable string format. This makes the "hex"
71      * readable!
72      *
73      * @return result String buffer in String format
74      * @param in
75      * byte[] buffer to convert to string format
76      */

77     private static String JavaDoc byteArrayToHexString(byte[] bytes) {
78         byte ch = 0x00;
79         int i = 0;
80         if (bytes == null || bytes.length <= 0)
81             return null;
82
83         String JavaDoc pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
84                 "A", "B", "C", "D", "E", "F" };
85         StringBuffer JavaDoc out = new StringBuffer JavaDoc(bytes.length * 2);
86
87         while (i < bytes.length) {
88             ch = (byte) (bytes[i] & 0xF0); // Strip off high nibble
89
ch = (byte) (ch >>> 4);
90             // shift the bits down
91
ch = (byte) (ch & 0x0F);
92             // must do this is high order bit is on!
93
out.append(pseudo[(int) ch]);
94             // convert the nibble to a String Character
95
ch = (byte) (bytes[i] & 0x0F);
96             // Strip off low nibble
97
out.append(pseudo[(int) ch]);
98             // convert the nibble to a String Character
99
i++;
100         }
101         return new String JavaDoc(out.toString());
102     }
103
104 }
105
Popular Tags