MD5的3中写法记录

来源:互联网 发布:伊斯兰教在中国知乎 编辑:程序博客网 时间:2024/05/22 17:06

介绍3种MD5的写法

第一种

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Location {





   public static String getMd5(String pInput) {  
       try {  
           MessageDigest lDigest = MessageDigest.getInstance("MD5");  
           lDigest.update(pInput.getBytes());  
           BigInteger lHashInt = new BigInteger(1, lDigest.digest());  
           return String.format("%1$032X", lHashInt);  
       } catch (NoSuchAlgorithmException lException) {  
           throw new RuntimeException(lException);  
       }  
   }  


}


第二种

import java.security.MessageDigest;


public class MD5Utils {

public static String getMD5(String message) {  
   String md5str = "";  
   try {  
       // 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象  
       MessageDigest md = MessageDigest.getInstance("MD5");  
 
       // 2 将消息变成byte数组  
       byte[] input = message.getBytes();  
 
       // 3 计算后获得字节数组,这就是那128位了  
       byte[] buff = md.digest(input);  
 
       // 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串  
       md5str = bytesToHex(buff);  
 
   } catch (Exception e) {  
       e.printStackTrace();  
   }  
   return md5str;  
   }  
 
   /** 
    * 二进制转十六进制 
    *  
    * @param bytes 
    * @return 
    */  
   public static String bytesToHex(byte[] bytes) {  
   StringBuffer md5str = new StringBuffer();  
   // 把数组每一字节换成16进制连成md5字符串  
   int digital;  
   for (int i = 0; i < bytes.length; i++) {  
       digital = bytes[i];  
 
       if (digital < 0) {  
       digital += 256;  
       }  
       if (digital < 16) {  
       md5str.append("0");  
       }  
       md5str.append(Integer.toHexString(digital));  
   }  
   return md5str.toString().toUpperCase();  
   }  
 


}

第三种

public class MD5 {
public static String getMD5(byte[] source) {
       String s = null;
       char hexDigits[] = {
               // ��4���ֽ�ת���� 16 ���Ʊ�ʾ���ַ�
               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
       try {
           java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
           md.update(source);
           byte tmp[] = md.digest(); // MD5 �ļ�������һ�� 128 λ�ij�����
           // ���ֽڱ�ʾ���� 16 ���ֽ�
           char str[] = new char[16 * 2]; // ÿ���ֽ��� 16 ���Ʊ�ʾ�Ļ���ʹ��}���ַ� // ���Ա�ʾ�� 16 ������Ҫ 32 ���ַ�
           int k = 0;
           // ��ʾת������ж�Ӧ���ַ�λ��
           for (int i = 0; i < 16; i++) {
               // �ӵ�һ���ֽڿ�ʼ���� MD5 ��ÿһ���ֽ� // ת���� 16 �����ַ��ת��
               byte byte0 = tmp[i]; // ȡ�� i ���ֽ�
               str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // ȡ�ֽ��и� 4 λ������ת��, // >>> // Ϊ�߼����ƣ������λһ������
               str[k++] = hexDigits[byte0 & 0xf]; // ȡ�ֽ��е� 4 λ������ת��
           }
           s = new String(str); // ����Ľ��ת��Ϊ�ַ�
       } catch (Exception e) {
           e.printStackTrace();
       }


       return s;


   }


}