目錄: (1)需要導入的包 (2)MD5Util類 (3)使用舉例 (1)需要導入的包 (2)MD5Util類 ...
目錄
(1)需要導入的包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
(2)MD5Util類
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MD5Util {
/**
* 字元串簽名
* @param text 需要簽名的字元串
* @param key 密鑰
* @param charset 編碼格式
* @return 簽名結果
*/
public static String sign(String text, String key, String charset) {
//拼接key
text = text + key;
return DigestUtils.md5Hex(getContentBytes(text, charset));
}
/**
* 根據參數map簽名
* @param map 有序map
* @param key
* @param charset
* @return
*/
public static String sign(TreeMap<String, Object> map, String key, String charset){
Set<String> keySet = map.keySet();
StringBuilder sb = new StringBuilder();
for (String mapKey : keySet){
String value = (String)map.get(key);
sb.append(key).append("=").append(value).append("&");
}
return sign(sb.toString(), key, charset);
}
/**
* 根據參數map簽名
* @param map hashMap
* @param key
* @param charset
* @return
*/
public static String sign1(Map<String, Object> map, String key, String charset){
Set<String> keySet = map.keySet();
StringBuilder sb = new StringBuilder();
for (String mapKey : keySet){
String value = (String)map.get(key);
sb.append(key).append("=").append(value).append("&");
}
return sign(sb.toString(), key, charset);
}
/**
* 根據參數map簽名
* @param map hashMap
* @param key
* @param charset
* @return
*/
public static String sign(Map<String, String> map, String key, String charset){
Set<String> keySet = map.keySet();
StringBuilder sb = new StringBuilder();
for (String mapKey : keySet){
String value = (String)map.get(key);
sb.append(key).append("=").append(value).append("&");
}
return sign(sb.toString(), key, charset);
}
/**
* 根據字元串獲取byte[]
* @param content
* @param charset
* @return
*/
private static byte[] getContentBytes(String content, String charset){
if (StringUtils.isEmpty(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("轉碼過程中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset);
}
}
/**
* 簽名,不用加密碼
* @param origin
* @param charsetname
* @return
*/
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}
(3)使用舉例
//加密
//要加密的數據map<String, String>
Map<String, String> data = new TreeMap<String, String>();
String key="key123";//加密key
String sign = MD5Util.sign(data, key, "utf-8");
//要加密的數據map<String, Object>
Map<String, Object> result = new TreeMap<String, Object>();
String key="key123";//加密key
String sign = MD5Util.sign1(data, key, "utf-8");
//不用密碼的加密,加密一個String
String sign=MD5Util.MD5Encode(mchkey, "utf-8");