签名工具注释修改
This commit is contained in:
@@ -11,72 +11,16 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* @description: 生成签名
|
||||
* @description: 签名工具
|
||||
* @author: dimengzhe
|
||||
* @date: 2024/11/28
|
||||
**/
|
||||
public class SignatureUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 生成请求签名
|
||||
*
|
||||
* @param parameters 请求参数
|
||||
* @param secret 密钥
|
||||
* @return 签名
|
||||
*/
|
||||
public static String generateSignature(Map<String, String> parameters, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
||||
//1.对参数进行排序
|
||||
Map<String, String> treeMap = new TreeMap<>(parameters);
|
||||
// 2. 拼接参数字符串
|
||||
String content = joinParameters(treeMap);
|
||||
|
||||
// 3. 将密钥加在参数字符串的前后
|
||||
content = secret + content + secret;
|
||||
|
||||
// 4. 计算签名 (MD5)
|
||||
return md5(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接参数字符串
|
||||
*
|
||||
* @param tree 排序后的参数
|
||||
* @return 拼接后的参数字符串
|
||||
*/
|
||||
public static String joinParameters(Map<String, String> tree) throws UnsupportedEncodingException {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : tree.entrySet()) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append("&");
|
||||
}
|
||||
builder.append(entry.getKey()).append("=");
|
||||
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算 MD5
|
||||
*
|
||||
* @param content 要计算 MD5 的字符串
|
||||
* @return MD5 值
|
||||
*/
|
||||
public 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证签名是否正确
|
||||
*
|
||||
* @param parameters 请求参数
|
||||
* @param secret 密钥
|
||||
* @param secret 私钥,生成签名时使用,不允许在请求参数中出现
|
||||
* @return 是否验证通过
|
||||
*/
|
||||
public static ResultBean<Boolean> validateSignature(Map<String, String> parameters, String secret) {
|
||||
@@ -108,4 +52,60 @@ public class SignatureUtil {
|
||||
}
|
||||
return rb.success().setData(valid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*
|
||||
* @param parameters 请求参数
|
||||
* @param secret 私钥
|
||||
* @return 签名字符串
|
||||
*/
|
||||
private static String generateSignature(Map<String, String> parameters, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
||||
//1.对参数进行排序
|
||||
Map<String, String> treeMap = new TreeMap<>(parameters);
|
||||
// 2. 拼接参数字符串
|
||||
String content = joinParameters(treeMap);
|
||||
|
||||
// 3. 将密钥加在参数字符串的前后
|
||||
content = secret + content + secret;
|
||||
|
||||
// 4. 计算签名 (MD5)
|
||||
return md5(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接参数字符串
|
||||
*
|
||||
* @param tree 排序后的参数
|
||||
* @return 拼接后的参数字符串
|
||||
*/
|
||||
private static String joinParameters(Map<String, String> tree) throws UnsupportedEncodingException {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : tree.entrySet()) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append("&");
|
||||
}
|
||||
builder.append(entry.getKey()).append("=");
|
||||
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user