|
@@ -1,5 +1,7 @@
|
|
package com.zanxiang.sdk.common.util;
|
|
package com.zanxiang.sdk.common.util;
|
|
|
|
|
|
|
|
+import com.zanxiang.common.exception.BaseException;
|
|
|
|
+
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
@@ -81,8 +83,12 @@ public class WxPayUtil {
|
|
* @param key API密钥
|
|
* @param key API密钥
|
|
* @return 签名
|
|
* @return 签名
|
|
*/
|
|
*/
|
|
- public static String generateSignature(final Map<String, String> data, String key) throws Exception {
|
|
|
|
- return generateSignature(data, key, SIGN_MD5);
|
|
|
|
|
|
+ public static String generateSignature(final Map<String, String> data, String key) {
|
|
|
|
+ try {
|
|
|
|
+ return generateSignature(data, key, SIGN_MD5);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new BaseException("计算腾讯微信接口签名异常");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -93,7 +99,7 @@ public class WxPayUtil {
|
|
* @param signType 签名加密方式
|
|
* @param signType 签名加密方式
|
|
* @return 签名
|
|
* @return 签名
|
|
*/
|
|
*/
|
|
- public static String generateSignature(final Map<String, String> data, String key, String signType) throws Exception {
|
|
|
|
|
|
+ private static String generateSignature(final Map<String, String> data, String key, String signType) throws Exception {
|
|
Set<String> keySet = data.keySet();
|
|
Set<String> keySet = data.keySet();
|
|
String[] keyArray = keySet.toArray(new String[0]);
|
|
String[] keyArray = keySet.toArray(new String[0]);
|
|
Arrays.sort(keyArray);
|
|
Arrays.sort(keyArray);
|
|
@@ -141,7 +147,7 @@ public class WxPayUtil {
|
|
* @return MD5结果
|
|
* @return MD5结果
|
|
*/
|
|
*/
|
|
private static String MD5(String data) throws Exception {
|
|
private static String MD5(String data) throws Exception {
|
|
- java.security.MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
|
|
+ java.security.MessageDigest md = MessageDigest.getInstance(SIGN_MD5);
|
|
byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));
|
|
byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (byte item : array) {
|
|
for (byte item : array) {
|
|
@@ -159,8 +165,8 @@ public class WxPayUtil {
|
|
* @throws Exception : 异常
|
|
* @throws Exception : 异常
|
|
*/
|
|
*/
|
|
public static String HMACSHA256(String data, String key, boolean upperCase) throws Exception {
|
|
public static String HMACSHA256(String data, String key, boolean upperCase) throws Exception {
|
|
- Mac sha256HMAC = Mac.getInstance("HmacSHA256");
|
|
|
|
- SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
|
|
|
+ Mac sha256HMAC = Mac.getInstance(SIGN_HMACSHA256);
|
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SIGN_HMACSHA256);
|
|
sha256HMAC.init(secretKey);
|
|
sha256HMAC.init(secretKey);
|
|
byte[] array = sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
|
byte[] array = sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|