android下md5加密

2013/01/17 22:53
阅读数 164
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
         'A', 'B', 'C', 'D', 'E', 'F' };
 
        public static String toHexString(byte[] b) {  //String to  byte
                  StringBuilder sb = new StringBuilder(b.length * 2);  
                  for (int i = 0; i < b.length; i++) {  
                      sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);  
                      sb.append(HEX_DIGITS[b[i] & 0x0f]);  
                  }  
                  return sb.toString();  
         }
           public String md5(String s) {
                 try {
                     // Create MD5 Hash
                     MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
                     digest.update(s.getBytes());
                     byte messageDigest[] = digest.digest();
                                 
                     return toHexString(messageDigest);
                 } catch (NoSuchAlgorithmException e) {
                     e.printStackTrace();
                 }
                         
                 return "";
             }
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部