test: verify lingxing track key vector
This commit is contained in:
@@ -87,15 +87,18 @@ public class LingxingDirectClient {
|
||||
}
|
||||
private static String text(JsonNode node, String key) { return node.path(key).isMissingNode() ? null : node.path(key).asText(null); }
|
||||
private static java.math.BigDecimal decimal(JsonNode node, String key) { return node.path(key).isNumber() ? node.path(key).decimalValue() : null; }
|
||||
private String trackKey(String method, String json) {
|
||||
if (properties.getTrackKeyV2SecretBase64() == null || properties.getTrackKeyV2SecretBase64().isBlank()) throw new IllegalStateException("未配置领星 Track-Key v2 密钥");
|
||||
static String buildTrackKey(String method, String json, String secretBase64) {
|
||||
if (secretBase64 == null || secretBase64.isBlank()) throw new IllegalStateException("未配置领星 Track-Key v2 密钥");
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(json.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hex = new StringBuilder(); for (byte b : digest) hex.append(String.format(Locale.ROOT, "%02x", b));
|
||||
Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(Base64.getDecoder().decode(properties.getTrackKeyV2SecretBase64()), "HmacSHA256"));
|
||||
byte[] key = Base64.getDecoder().decode(secretBase64);
|
||||
if (key.length != 44) throw new IllegalStateException("领星 Track-Key v2 密钥解码后必须是 44 字节");
|
||||
Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key, "HmacSHA256"));
|
||||
return "v2:" + Base64.getEncoder().encodeToString(mac.doFinal((method.toUpperCase(Locale.ROOT) + hex).getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (Exception ex) { throw new IllegalStateException("领星 Track-Key 生成失败", ex); }
|
||||
}
|
||||
private String trackKey(String method, String json) { return buildTrackKey(method, json, properties.getTrackKeyV2SecretBase64()); }
|
||||
private String canonical(JsonNode node) throws Exception { return mapper.writeValueAsString(sort(node)); }
|
||||
private JsonNode sort(JsonNode node) {
|
||||
if (!node.isObject()) return node;
|
||||
|
||||
Reference in New Issue
Block a user