package com.nova.sankuai.domain.api.jiniu;
|
|
import java.nio.charset.StandardCharsets;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
/**
|
* 类描述:对密码进行加密包装处理<br/>
|
* 创建人:gaoqiang<br/>
|
* 创建时间:2015-4-17下午6:30:20
|
*/
|
public class EncryptUtil {
|
|
/**
|
* 方法描述:md5加密
|
* @param inputText
|
* @return
|
* String
|
*/
|
public static String md5(String inputText){
|
return encrypt(inputText, "md5");
|
}
|
|
/**
|
* 方法描述:<br/>
|
* @return
|
* String
|
*/
|
private static String encrypt(String inputText, String algorithmName){
|
if (inputText == null || "".equals(inputText.trim())) {
|
throw new IllegalArgumentException("请输入要加密的内容");
|
}
|
if (algorithmName == null || "".equals(algorithmName.trim())) {
|
algorithmName = "md5";
|
}
|
String encryptText = null;
|
try {
|
MessageDigest m = MessageDigest.getInstance(algorithmName);
|
m.update(inputText.getBytes(StandardCharsets.UTF_8));
|
return hex(m.digest());
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
*
|
* 方法描述:返回十六进制字符串
|
* @param arr
|
* @return
|
* String
|
*/
|
private static String hex(byte[] arr) {
|
StringBuffer sb = new StringBuffer();
|
for (byte b : arr) {
|
sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
|
}
|
return sb.toString();
|
}
|
|
|
|
|
}
|