package com.library.app.instrument;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author admin
* @see DesUtil
*/
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' };
/**
* MD5
*
* @param b
* byte数组
* @return String byte数组处理后字符串
*/
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();
}
/**
* MD5运算
*
* @param s
* 传入明文
* @return String 返回密文
*/
public static 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 s;
}
}
MD5加密
最新推荐文章于 2024-12-27 11:41:01 发布