
2 changed files with 0 additions and 99 deletions
@ -1,71 +0,0 @@ |
|||||
import java.io.UnsupportedEncodingException; |
|
||||
import java.security.MessageDigest; |
|
||||
import java.security.NoSuchAlgorithmException; |
|
||||
import java.util.Map; |
|
||||
import java.util.TreeMap; |
|
||||
|
|
||||
/** |
|
||||
* @description: 生成签名 |
|
||||
* @author: dimengzhe |
|
||||
* @date: 2024/11/28 |
|
||||
**/ |
|
||||
public class SignatureUtil { |
|
||||
|
|
||||
|
|
||||
/** |
|
||||
* 生成请求签名 |
|
||||
* |
|
||||
* @param parameters 请求参数 |
|
||||
* @param appKey 应用的 AppKey |
|
||||
* @param secret 密钥 |
|
||||
* @return 签名 |
|
||||
*/ |
|
||||
public static String generateSignature(Map<String, String> parameters, String appKey, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException { |
|
||||
// 1. 使用 TreeMap 按照字典顺序对参数进行排序
|
|
||||
Map<String, String> sortedParams = new TreeMap<>(parameters); |
|
||||
sortedParams.put("_app", appKey); // 添加应用的 AppKey
|
|
||||
sortedParams.put("_t", String.valueOf(System.currentTimeMillis())); // 添加时间戳
|
|
||||
|
|
||||
// 2. 拼接参数字符串
|
|
||||
String content = joinParameters(sortedParams); |
|
||||
|
|
||||
// 3. 将密钥加在参数字符串的前后
|
|
||||
content = secret + content + secret; |
|
||||
|
|
||||
// 4. 计算签名 (MD5)
|
|
||||
return md5(content); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 拼接参数字符串 |
|
||||
* |
|
||||
* @param parameters 排序后的参数 |
|
||||
* @return 拼接后的参数字符串 |
|
||||
*/ |
|
||||
private static String joinParameters(Map<String, String> parameters) throws UnsupportedEncodingException { |
|
||||
StringBuilder builder = new StringBuilder(); |
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
|
||||
if (builder.length() > 0) { |
|
||||
builder.append("&"); |
|
||||
} |
|
||||
builder.append(entry.getKey()).append("=").append(entry.getValue()); |
|
||||
} |
|
||||
return builder.toString(); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 计算 MD5 |
|
||||
* |
|
||||
* @param content 要计算 MD5 的字符串 |
|
||||
* @return MD5 值 |
|
||||
*/ |
|
||||
private static String md5(String content) throws NoSuchAlgorithmException { |
|
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
||||
byte[] bytes = md.digest(content.getBytes()); |
|
||||
StringBuilder sb = new StringBuilder(); |
|
||||
for (byte b : bytes) { |
|
||||
sb.append(String.format("%02x", b)); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
} |
|
@ -1,28 +0,0 @@ |
|||||
import java.io.UnsupportedEncodingException; |
|
||||
import java.security.NoSuchAlgorithmException; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
/** |
|
||||
* @description: 验证签名方法 |
|
||||
* @author: dimengzhe |
|
||||
* @date: 2024/11/28 |
|
||||
**/ |
|
||||
public class SignatureValidator { |
|
||||
|
|
||||
/** |
|
||||
* 验证签名是否正确 |
|
||||
* |
|
||||
* @param parameters 请求参数 |
|
||||
* @param appKey 应用的 AppKey |
|
||||
* @param secret 密钥 |
|
||||
* @param receivedSignature 请求中的签名 |
|
||||
* @return 是否验证通过 |
|
||||
*/ |
|
||||
public static boolean validateSignature(Map<String, String> parameters, String appKey, String secret, String receivedSignature) throws UnsupportedEncodingException, NoSuchAlgorithmException { |
|
||||
// 1. 重新生成签名
|
|
||||
String calculatedSignature = SignatureUtil.generateSignature(parameters, appKey, secret); |
|
||||
|
|
||||
// 2. 比较计算出来的签名和请求中的签名
|
|
||||
return calculatedSignature.equals(receivedSignature); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue