1 25 package org.jresearch.gossip.util; 26 27 import java.security.MessageDigest ; 28 29 import org.apache.log.Logger; 30 import org.jresearch.gossip.exception.SystemException; 31 import org.jresearch.gossip.log.avalon.JGossipLog; 32 33 39 public class MD5Digest { 40 41 50 public static String digest(String username, String password) { 51 try { 52 MessageDigest 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 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) { 64 } 65 return null; 66 } 67 } 68 69 77 private static String byteArrayToHexString(byte[] bytes) { 78 byte ch = 0x00; 79 int i = 0; 80 if (bytes == null || bytes.length <= 0) 81 return null; 82 83 String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 84 "A", "B", "C", "D", "E", "F" }; 85 StringBuffer out = new StringBuffer (bytes.length * 2); 86 87 while (i < bytes.length) { 88 ch = (byte) (bytes[i] & 0xF0); ch = (byte) (ch >>> 4); 90 ch = (byte) (ch & 0x0F); 92 out.append(pseudo[(int) ch]); 94 ch = (byte) (bytes[i] & 0x0F); 96 out.append(pseudo[(int) ch]); 98 i++; 100 } 101 return new String (out.toString()); 102 } 103 104 } 105 | Popular Tags |