|
@@ -14,54 +14,32 @@ import java.security.NoSuchAlgorithmException;
|
|
*/
|
|
*/
|
|
public class TestService {
|
|
public class TestService {
|
|
|
|
|
|
-// public static String calcPaySig(String uri, String postBody, String appKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
-// String needSignMsg = uri + "&" + postBody;
|
|
|
|
-// Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
|
-// SecretKeySpec secretKey = new SecretKeySpec(appKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
|
-// hmacSha256.init(secretKey);
|
|
|
|
-// byte[] signatureBytes = hmacSha256.doFinal(needSignMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
|
-// return bytesToHex(signatureBytes);
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
-// public static String calcSignature(String postBody, String sessionKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
-// Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
|
-// SecretKeySpec secretKey = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
|
-// hmacSha256.init(secretKey);
|
|
|
|
-// byte[] signatureBytes = hmacSha256.doFinal(postBody.getBytes(StandardCharsets.UTF_8));
|
|
|
|
-// return bytesToHex(signatureBytes);
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
-// private static String bytesToHex(byte[] bytes) {
|
|
|
|
-// StringBuilder hexString = new StringBuilder();
|
|
|
|
-// for (byte b : bytes) {
|
|
|
|
-// String hex = Integer.toHexString(0xff & b);
|
|
|
|
-// if (hex.length() == 1) {
|
|
|
|
-// hexString.append('0');
|
|
|
|
-// }
|
|
|
|
-// hexString.append(hex);
|
|
|
|
-// }
|
|
|
|
-// return hexString.toString();
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
-// public static void main(String[] args) throws Exception {
|
|
|
|
-// String a = "payKey=2d71711b38fef06b80e159a35297bcf7&userId=51&money=10.0&time=1664553600000&serverId=001&orderId=1578307332482088961";
|
|
|
|
-// System.out.println(MD5(a));
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
-//
|
|
|
|
-// /**
|
|
|
|
-// * MD5加密
|
|
|
|
-// *
|
|
|
|
-// * @param data 待处理数据
|
|
|
|
-// * @return MD5结果
|
|
|
|
-// */
|
|
|
|
-// public static String MD5(String data) throws Exception {
|
|
|
|
-// java.security.MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
-// byte[] array = md.digest(data.getBytes("UTF-8"));
|
|
|
|
-// StringBuilder sb = new StringBuilder();
|
|
|
|
-// for (byte item : array) {
|
|
|
|
-// sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
|
|
|
|
-// }
|
|
|
|
-// return sb.toString().toUpperCase();
|
|
|
|
-// }
|
|
|
|
|
|
+ public static String calcPaySig(String uri, String postBody, String appKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
+ String needSignMsg = uri + "&" + postBody;
|
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(appKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
|
+ hmacSha256.init(secretKey);
|
|
|
|
+ byte[] signatureBytes = hmacSha256.doFinal(needSignMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ return bytesToHex(signatureBytes);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String calcSignature(String postBody, String sessionKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
|
+ hmacSha256.init(secretKey);
|
|
|
|
+ byte[] signatureBytes = hmacSha256.doFinal(postBody.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ return bytesToHex(signatureBytes);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String bytesToHex(byte[] bytes) {
|
|
|
|
+ StringBuilder hexString = new StringBuilder();
|
|
|
|
+ for (byte b : bytes) {
|
|
|
|
+ String hex = Integer.toHexString(0xff & b);
|
|
|
|
+ if (hex.length() == 1) {
|
|
|
|
+ hexString.append('0');
|
|
|
|
+ }
|
|
|
|
+ hexString.append(hex);
|
|
|
|
+ }
|
|
|
|
+ return hexString.toString();
|
|
|
|
+ }
|
|
}
|
|
}
|