Android获取String的MD5值

来源:互联网 发布:北方广电网络 编辑:程序博客网 时间:2024/05/27 09:45
  1. /** 
  2.  * 将字符串转成MD5值 
  3.  *  
  4.  *
  5.  */  
  6. public static String stringToMD5(String string) {  
  7.     byte[] hash;  
  8.   
  9.     try {  
  10.         hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));  
  11.     } catch (NoSuchAlgorithmException e) {  
  12.         e.printStackTrace();  
  13.         return null;  
  14.     } catch (UnsupportedEncodingException e) {  
  15.         e.printStackTrace();  
  16.         return null;  
  17.     }  
  18.   
  19.     StringBuilder hex = new StringBuilder(hash.length * 2);  
  20.     for (byte b : hash) {  
  21.         if ((b & 0xFF) < 0x10)  
  22.             hex.append("0");  
  23.         hex.append(Integer.toHexString(b & 0xFF));  
  24.     }  
  25.   
  26.     return hex.toString();  

0 0
原创粉丝点击