3 changed files with 113 additions and 56 deletions
-
59src/main/java/com/example/demo/controller/cash/SignatureGenerator.java
-
74src/main/java/com/example/demo/serviceImpl/cash/BankServiceImpl.java
@ -0,0 +1,59 @@ |
|||
package com.example.demo.controller.cash; |
|||
|
|||
import javax.crypto.Mac; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.security.InvalidKeyException; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Base64; |
|||
import java.util.Random; |
|||
|
|||
public class SignatureGenerator { |
|||
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException { |
|||
// 配置参数 |
|||
String key = "3E04ZUCKFmQKrW0uoBa89QKIJWYoU9OX"; |
|||
String secret = "ZLtBPgfMIT4HXg25SoVuCyUQZ6GtSv9UFmDmYaoVSKS"; |
|||
|
|||
// 生成ClientRequestId(1-10000000的随机数) |
|||
long clientRequestId = new Random().nextInt(10000000) + 1; |
|||
|
|||
// 获取当前时间戳(毫秒) |
|||
long time = System.currentTimeMillis(); |
|||
|
|||
// 构造原始签名数据 |
|||
String rawSignature = key + clientRequestId + time; |
|||
System.out.println("rawdata: " + rawSignature); |
|||
|
|||
// 计算HMAC-SHA256 |
|||
Mac sha256Hmac = Mac.getInstance("HmacSHA256"); |
|||
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); |
|||
sha256Hmac.init(secretKey); |
|||
byte[] hmacBytes = sha256Hmac.doFinal(rawSignature.getBytes(StandardCharsets.UTF_8)); |
|||
|
|||
// 转换为十六进制字符串(对应CryptoJS的toString()) |
|||
StringBuilder hmacHex = new StringBuilder(); |
|||
for (byte b : hmacBytes) { |
|||
hmacHex.append(String.format("%02x", b)); |
|||
} |
|||
System.out.println("addingHMAC: " + hmacHex.toString()); |
|||
|
|||
// Base64编码 |
|||
String computedHmac = Base64.getEncoder().encodeToString(hmacBytes); |
|||
System.out.println("addingBase64: " + computedHmac); |
|||
|
|||
// Base64解码(转回字节数组) |
|||
byte[] decodedBytes = Base64.getDecoder().decode(computedHmac); |
|||
StringBuilder decodedHex = new StringBuilder(); |
|||
for (byte b : decodedBytes) { |
|||
decodedHex.append(String.format("%02x", b)); |
|||
} |
|||
System.out.println("base64decode: " + decodedHex.toString()); |
|||
|
|||
// 打印所有环境变量参数 |
|||
System.out.println("\n环境变量参数:"); |
|||
System.out.println("key: " + key); |
|||
System.out.println("time: " + time); |
|||
System.out.println("signature: " + computedHmac); |
|||
System.out.println("ClientRequestId: " + clientRequestId); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue