67 lines
1.8 KiB
Java
67 lines
1.8 KiB
Java
package com.peanut.common.utils;
|
|
|
|
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
import java.security.MessageDigest;
|
|
|
|
public class KdUtils {
|
|
|
|
|
|
|
|
/**
|
|
* MD5加密
|
|
* str 内容
|
|
* charset 编码方式
|
|
* @throws Exception
|
|
*/
|
|
public static String MD5(String str, String charset) throws Exception {
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
md.update(str.getBytes(charset));
|
|
byte[] result = md.digest();
|
|
StringBuffer sb = new StringBuffer(32);
|
|
for (int i = 0; i < result.length; i++) {
|
|
int val = result[i] & 0xff;
|
|
if (val <= 0xf) {
|
|
sb.append("0");
|
|
}
|
|
sb.append(Integer.toHexString(val));
|
|
}
|
|
return sb.toString().toLowerCase();
|
|
}
|
|
|
|
/**
|
|
* base64编码
|
|
* str 内容
|
|
* charset 编码方式
|
|
* @throws UnsupportedEncodingException
|
|
*/
|
|
public static String base64(String str, String charset) throws UnsupportedEncodingException{
|
|
String encoded = Base64.encode(str.getBytes(charset));
|
|
return encoded;
|
|
}
|
|
|
|
public static String urlEncoder(String str, String charset) throws UnsupportedEncodingException{
|
|
return URLEncoder.encode(str, charset);
|
|
}
|
|
|
|
/**
|
|
* 电商Sign签名生成
|
|
* content 内容
|
|
* keyValue ApiKey
|
|
* charset 编码方式
|
|
* @throws UnsupportedEncodingException ,Exception
|
|
* @return DataSign签名
|
|
*/
|
|
public static String encrypt (String content, String keyValue, String charset) throws Exception
|
|
{
|
|
if (keyValue != null)
|
|
{
|
|
return base64(MD5(content + keyValue, charset), charset);
|
|
}
|
|
return base64(MD5(content, charset), charset);
|
|
}
|
|
|
|
}
|