喜糖

移动开发工程师 。涉及 android、ios、jni

导航

【一找客户端】MD5封装

Posted on 2011-09-22 11:35  喜糖  阅读(399)  评论(0编辑  收藏  举报
public class MD5 {

private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

private 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 static String toMd5(String s) {
String res = "";
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
res = toHexString(messageDigest);
Log.d(Const.TAG, "MD5:defult_str="+s);
Log.d(Const.TAG, "MD5:convet_str"+res);
} catch (NoSuchAlgorithmException e) {
Log.e(Const.TAG, "MD5.toMd5|NoSuchAlgorithmException",e);
}
return res;
}
}