MD5加密算法
package com.mynj.utils;
import java.security.MessageDigest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 采用MD5加密
* @author hous
* @date 2015-11-5
*
*/
public class MD5Utils {
private static Logger logger = LoggerFactory.getLogger(MD5Utils.class);
/**
* MD5加密 生成32位md5码
* @param 待加密字符串
* @return 返回32位md5码
* @throws Exception
*/
public static String md5Encode(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
} catch (Exception e) {
logger.error("md5加密异常:{}", sourceStr, e);
}
return result;
}
public static void main(String[] args) {
System.out.println(md5Encode("www.shanshanbox.com"));
}
}