汎用簡易MD 5プログラム



import java.security.MessageDigest; 
/**
 * FileName:MD5.CLASS
 * Author:Gboshi
 * Create File Time:2006-07
 * */
public class Md5 { 

	/**
	* String Array?
	* @pram MyMiss
	* */
  private final static String[] MyMiss = { 
      "0", "1", "2", "3", "4", "5", "6", "7", 
      "8", "9", "a", "b", "c", "d", "e", "f"}; 

  public static String byteArrayToHexString(byte[] b) 
  { 
    StringBuffer sb = new StringBuffer(); 
    for (int i = 0; i < b.length; i++) 
    { 
    	sb.append(byteToHexString(b[i])); 
    } 
    return sb.toString(); 
  } 

  private static String byteToHexString(byte b) 
  { 
    int n = b; 
    if (n < 0) 
      n = 256 + n; 
    int d1 = n / 16; 
    int d2 = n % 16; 
    return MyMiss[d1] + MyMiss[d2]; 
  } 

  public static String md5(String origin)
  { 
    String rs = null; 
    try { 
      rs=new String(origin); 
      MessageDigest md = MessageDigest.getInstance("MD5"); 
      rs=byteArrayToHexString(md.digest(rs.getBytes())); 
    } 
    catch (Exception ex) { 
    	ex.printStackTrace();
    } 
    return rs; 
  }
  //Test
  public static void main(String agrs[]){
	  System.out.println(md5("admin"));
	  System.out.println(md5("ADMIN"));
  }
}