You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.7 KiB
97 lines
3.7 KiB
package common.ecommerce.lingxing.http; |
|
|
|
import common.ecommerce.spi.ChannelSecret; |
|
|
|
import javax.crypto.Cipher; |
|
import javax.crypto.spec.SecretKeySpec; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.util.ArrayList; |
|
import java.util.Base64; |
|
import java.util.Collections; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* 领星签名(2026 版官方算法,对照 openapi-sdk-java 的 ApiSign / AesUtil)。 |
|
* |
|
* <ol> |
|
* <li>业务参数 + {@code access_token} + {@code app_key} + {@code timestamp},按 key ASCII 升序</li> |
|
* <li>拼成 {@code k=v&k=v},value 做 trim;集合类型必须先 JSON 序列化再参与签名</li> |
|
* <li>MD5 32 位大写</li> |
|
* <li>AES/ECB/PKCS5Padding 加密,密钥是 appId(即 app_key),输出 Base64</li> |
|
* </ol> |
|
* |
|
* <p>注意:SDK 里 {@code ApiSign.sign(params, appSecret)} 的第二个形参命名有误, |
|
* 实际传的是 appId;appSecret 不参与签名。</p> |
|
*/ |
|
public final class LingxingSigner { |
|
|
|
private static final String AES_ECB = "AES/ECB/PKCS5Padding"; |
|
private static final char[] HEX = "0123456789ABCDEF".toCharArray(); |
|
|
|
private LingxingSigner() { |
|
} |
|
|
|
/** 便捷:appId 就是凭证里的 appKey。 */ |
|
public static String sign(Map<String, String> params, ChannelSecret secret) { |
|
return sign(params, secret.appKey()); |
|
} |
|
|
|
/** |
|
* @param params 参与签名的全部参数(含 access_token / app_key / timestamp),不含 sign |
|
* @param appId 应用 AppID,同时作为 AES 密钥,必须 16/24/32 字节 |
|
*/ |
|
public static String sign(Map<String, String> params, String appId) { |
|
return aesEcbBase64(md5HexUpper(canonical(params)), appId); |
|
} |
|
|
|
/** 拼接待签名原文:按 key 升序,跳过 null,value trim。 */ |
|
static String canonical(Map<String, String> params) { |
|
List<String> keys = new ArrayList<String>(params.keySet()); |
|
Collections.sort(keys); |
|
StringBuilder sb = new StringBuilder(); |
|
for (String key : keys) { |
|
String value = params.get(key); |
|
if (value == null) { |
|
continue; |
|
} |
|
if (sb.length() > 0) { |
|
sb.append('&'); |
|
} |
|
sb.append(key).append('=').append(value.trim()); |
|
} |
|
return sb.toString(); |
|
} |
|
|
|
static String md5HexUpper(String raw) { |
|
try { |
|
byte[] digest = MessageDigest.getInstance("MD5").digest(raw.getBytes(StandardCharsets.UTF_8)); |
|
char[] out = new char[digest.length * 2]; |
|
for (int i = 0; i < digest.length; i++) { |
|
int v = digest[i] & 0xFF; |
|
out[i * 2] = HEX[v >>> 4]; |
|
out[i * 2 + 1] = HEX[v & 0x0F]; |
|
} |
|
return new String(out); |
|
} catch (NoSuchAlgorithmException e) { |
|
throw new IllegalStateException("MD5 not available", e); |
|
} |
|
} |
|
|
|
static String aesEcbBase64(String plain, String appId) { |
|
byte[] key = appId.getBytes(StandardCharsets.UTF_8); |
|
if (key.length != 16 && key.length != 24 && key.length != 32) { |
|
throw new IllegalArgumentException( |
|
"领星 appId 必须是 16/24/32 字节(AES 密钥),实际 " + key.length + " 字节:" + appId); |
|
} |
|
try { |
|
Cipher cipher = Cipher.getInstance(AES_ECB); |
|
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES")); |
|
return Base64.getEncoder().encodeToString(cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8))); |
|
} catch (Exception e) { |
|
throw new IllegalStateException("领星签名 AES 加密失败", e); |
|
} |
|
} |
|
} |