Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 25th, 2010  |  syntax: Java  |  size: 1.09 KB  |  views: 82  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.UnsupportedEncodingException;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4.  
  5. public class AeSimpleMD5 {
  6.  
  7.     private static String convertToHex(byte[] data) {
  8.         StringBuffer buf = new StringBuffer();
  9.         for (int i = 0; i < data.length; i++) {
  10.             int halfbyte = (data[i] >>> 4) & 0x0F;
  11.             int two_halfs = 0;
  12.             do {
  13.                 if ((0 <= halfbyte) && (halfbyte <= 9))
  14.                     buf.append((char) ('0' + halfbyte));
  15.                 else
  16.                     buf.append((char) ('a' + (halfbyte - 10)));
  17.                 halfbyte = data[i] & 0x0F;
  18.             } while(two_halfs++ < 1);
  19.         }
  20.         return buf.toString();
  21.     }
  22.  
  23.     public static String MD5(String text)
  24.     throws NoSuchAlgorithmException, UnsupportedEncodingException  {
  25.         MessageDigest md;
  26.         md = MessageDigest.getInstance("MD5");
  27.         byte[] md5hash = new byte[32];
  28.         md.update(text.getBytes("iso-8859-1"), 0, text.length());
  29.         md5hash = md.digest();
  30.         return convertToHex(md5hash);
  31.     }
  32. }