package webraveler.util; import java.io.*; import java.security.*; /** * A simple little class for making generating MD5 hashes * of usernames -- for storing in password files -- easier. * * @author martinsa */ public class MD5Helper { /** * Returns the byte array as a hex string. * * @param array * @return */ private static String getHexString(byte[] array) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; i++) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3)); // sb.append(Integer.toHexString((array[i]))); }//for return sb.toString(); }//String getHexString(byte[]) /** * Returns the MD5 hash, as a hex string, of the given * message. * * @param message * @return */ public static String md5 (String message) throws Exception { try { MessageDigest md = MessageDigest.getInstance("MD5"); return getHexString(md.digest(message.getBytes(System.getProperty("file.encoding")))); } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { } throw new Exception ("md5: Error getting md5 hash!"); }//static String md5(String) /** * Print a few sample MD5 hashes to stdout * * @param args */ public static void main(String[] args) { try { System.out.println (md5 ("biatch") + " biatch"); System.out.println (md5 ("sam") + " sam"); } catch (Exception e) {} }//static void main }//class MD5Helper