字符串的MD5加密

来源:互联网 发布:淘宝申请维修流程 编辑:程序博客网 时间:2024/05/17 06:59
  1. public String hashKeyForDisk(String key) {  
  2.     String cacheKey;  
  3.     try {  
  4.         final MessageDigest mDigest = MessageDigest.getInstance("MD5");  
  5.         mDigest.update(key.getBytes());  
  6.         cacheKey = bytesToHexString(mDigest.digest());  
  7.     } catch (NoSuchAlgorithmException e) {  
  8.         cacheKey = String.valueOf(key.hashCode());  
  9.     }  
  10.     return cacheKey;  
  11. }  
  12.   
  13. private String bytesToHexString(byte[] bytes) {  
  14.     StringBuilder sb = new StringBuilder();  
  15.     for (int i = 0; i < bytes.length; i++) {  
  16.         String hex = Integer.toHexString(0xFF & bytes[i]);  
  17.         if (hex.length() == 1) {  
  18.             sb.append('0');  
  19.         }  
  20.         sb.append(hex);  
  21.     }  
  22.     return sb.toString();  
  23. }



0 0
原创粉丝点击