/* * MD5.java * * Created on May 15, 2007, 2:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package ecommercetest; import java.security.MessageDigest; /** * * @author gharding */ public class MD5 { /** Creates a new instance of MD5 */ public MD5() { } static final char[] HEX = "0123456789abcdef".toCharArray(); public static String bytesToHex (byte[] bytes) { if (bytes == null) return ""; StringBuffer sb = new StringBuffer (2 * bytes.length); for (int i = 0; i < bytes.length; ++i) { sb.append (HEX[(bytes[i]>>4)&0xF]); sb.append (HEX[bytes[i]&0xF]); } return sb.toString(); } public static String hash(String pass) { try { MessageDigest gen = MessageDigest.getInstance("MD5"); gen.update(pass.getBytes()); String hash = bytesToHex(gen.digest()); return hash; } catch (Exception e) {} return null; } }