import java.security.MessageDigest;
/**
* @version 1.0
* @author
* @date 2013-7-8
*/
public final class MD5 {
/**
* 使用的固定数字编码
* @author
*/
private final static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 获得指定字串的MD5编码
*
* @param target
* 目标字串
* @return MD5编码
*/
public final static String getMD5(final String target) {
try {
// 使用MD5创建MessageDigest对象
final MessageDigest mdTemp = MessageDigest.getInstance("MD5");
final byte[] md = mdTemp.digest(target.getBytes("UTF-8"));
final int len = md.length;
final char str[] = new char[len * 2];
byte b;
for (int i = 0, k = 0; i < len; i++) {
b = md[i];
str[k++] = MD5.hexDigits[(b >> 4) & 0xf];
str[k++] = MD5.hexDigits[b & 0xf];
}
return new String(str);
} catch (final Exception e) {
return null;
}
}
}
Java MD5
最新推荐文章于 2022-12-06 23:41:42 发布