This commit is contained in:
六如
2024-11-29 23:47:58 +08:00
parent 85b33e7c3d
commit 09330a7431
198 changed files with 1504 additions and 982 deletions

View File

@@ -12,7 +12,7 @@ import com.gitee.sop.sdk.common.SopSdkErrors;
import com.gitee.sop.sdk.exception.SdkException;
import com.gitee.sop.sdk.request.BaseRequest;
import com.gitee.sop.sdk.sign.SopSignException;
import com.gitee.sop.sdk.sign.SopSignature;
import com.gitee.sop.sdk.sign.SignUtil;
import com.gitee.sop.sdk.sign.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -155,10 +155,10 @@ public class OpenClient {
}
form.put(this.openConfig.getAppKeyName(), this.appId);
String content = SopSignature.getSignContent(form);
String content = SignUtil.getSignContent(form);
String sign;
try {
sign = SopSignature.rsaSign(content, privateKey, openConfig.getCharset(), openConfig.getSignType());
sign = SignUtil.rsaSign(content, privateKey, openConfig.getCharset(), openConfig.getSignType());
} catch (SopSignException e) {
throw new SdkException("构建签名错误", e);
}
@@ -168,7 +168,7 @@ public class OpenClient {
String resp = doExecute(this.url, requestForm, Collections.emptyMap());
if (log.isDebugEnabled()) {
log.debug("----------- 请求信息 -----------"
+ "\n请求参数" + SopSignature.getSignContent(form)
+ "\n请求参数" + SignUtil.getSignContent(form)
+ "\n待签名内容" + content
+ "\n签名(sign)" + sign
+ "\n----------- 返回结果 -----------"
@@ -274,7 +274,7 @@ public class OpenClient {
try {
String charset = this.openConfig.getCharset();
String signType = this.openConfig.getSignType();
return SopSignature.rsaCheck(signContent, sign, publicKeyPlatform, charset, signType);
return SignUtil.rsaCheck(signContent, sign, publicKeyPlatform, charset, signType);
} catch (SopSignException e) {
log.error("验证服务端sign出错signContent" + signContent, e);
return false;

View File

@@ -22,10 +22,9 @@ import java.util.List;
import java.util.Map;
/**
*
* @author runzhi
* 签名工具
*/
public class SopSignature {
public class SignUtil {
/** RSA最大加密明文大小 */
private static final int MAX_ENCRYPT_BLOCK = 117;
@@ -49,20 +48,19 @@ public class SopSignature {
/**
*
* 获取签名内容
* @param sortedParams
* @return
*/
public static String getSignContent(Map<String, ?> sortedParams) {
StringBuffer content = new StringBuffer();
StringBuilder content = new StringBuilder();
List<String> keys = new ArrayList<String>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
for (String key : keys) {
String value = String.valueOf(sortedParams.get(key));
if (StringUtils.areNotEmpty(key, value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
index++;
}
}
@@ -71,7 +69,7 @@ public class SopSignature {
/**
* rsa内容签名
*
*
* @param content
* @param privateKey
* @param charset
@@ -96,7 +94,7 @@ public class SopSignature {
/**
* sha256WithRsa 加签
*
*
* @param content
* @param privateKey
* @param charset
@@ -132,7 +130,7 @@ public class SopSignature {
/**
* sha1WithRsa 加签
*
*
* @param content
* @param privateKey
* @param charset
@@ -237,12 +235,12 @@ public class SopSignature {
return rsaCheckContent(content, sign, publicKey, charset);
}
public static boolean rsaCheckV1(Map<String, String> params, String publicKey,
String charset,String signType) throws SopSignException {
String sign = params.get("sign");
String content = getSignCheckContentV1(params);
return rsaCheck(content, sign, publicKey, charset,signType);
}
@@ -253,12 +251,12 @@ public class SopSignature {
return rsaCheckContent(content, sign, publicKey, charset);
}
public static boolean rsaCheckV2(Map<String, ?> params, String publicKey,
String charset,String signType) throws SopSignException {
String sign = String.valueOf(params.get("sign"));
String content = getSignCheckContentV2(params);
return rsaCheck(content, sign, publicKey, charset,signType);
}
@@ -379,7 +377,7 @@ public class SopSignature {
return bizContent;
}
/**
* 验签并解密
* <p>
@@ -470,7 +468,7 @@ public class SopSignature {
}
return sb.toString();
}
/**
* 加密并签名<br>
* <b>目前适用于公众号</b>
@@ -530,7 +528,7 @@ public class SopSignature {
/**
* 公钥加密
*
*
* @param content 待加密内容
* @param publicKey 公钥
* @param charset 字符集如UTF-8, GBK, GB2312
@@ -551,7 +549,7 @@ public class SopSignature {
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
@@ -575,7 +573,7 @@ public class SopSignature {
/**
* 私钥解密
*
*
* @param content 待解密内容
* @param privateKey 私钥
* @param charset 字符集如UTF-8, GBK, GB2312
@@ -597,7 +595,7 @@ public class SopSignature {
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);