Class Sm2SignGenerator
java.lang.Object
tech.riemann.nif.web.sign.Sm2SignGenerator
- All Implemented Interfaces:
SignGenerator
国密SM2签名生成器(非对称)
签名流程与 HMAC 共用 SignGenerator.buildSignContent(Map, String, String, String) 的规范化规则,
仅最后的签名步骤改用 SM2 私钥签名(SM3WithSM2)、公钥验签。
与 HMAC 的差异:HMAC 使用对称 secret;本类使用非对称密钥对(私钥签名、公钥验签)。
密钥来源通过 AppKeyProvider 注入,可对接 KMS/密钥库。
- Author:
- Kerbores(kerbores@riemann.tech)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription生成签名generate(Map<String, String> params, String secret, String timestamp, String requestPath, String nonce) 生成签名(完整参数)获取支持的算法sign(String content, PrivateKey privateKey) 使用密钥对象签名(非对称算法专用)boolean使用密钥对象验签(非对称算法专用)boolean验证签名booleanverify(Map<String, String> params, String sign, String secret, String timestamp, String requestPath, String nonce) 验证签名(完整参数)Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SignGenerator
buildSignContent, bytesToHex, calculateParamsMd5
-
Constructor Details
-
Sm2SignGenerator
public Sm2SignGenerator()
-
-
Method Details
-
generate
Description copied from interface:SignGenerator生成签名签名生成流程: 1. 按ASCII排序参数 2. 拼接成
key1=value1&key2=value2格式 3. 在末尾追加密钥 4. 计算哈希值- Specified by:
generatein interfaceSignGenerator- Parameters:
params- 业务参数(已过滤空 * @param secret 密钥- Returns:
- 签名字符串(十六进制)
-
generate
public String generate(Map<String, String> params, String secret, String timestamp, String requestPath, String nonce) Description copied from interface:SignGenerator生成签名(完整参数)签名内容组成(按字典序排序后连接): 1. 请求参数的SHA-256摘要(paramsMd5,字段名沿用既有结构兼容) 2. 应用密钥(appSecret) 3. 时间戳(timestamp) 4. 请求路径(requestPath) 5. 随机字符串(nonce)
- Specified by:
generatein interfaceSignGenerator- Parameters:
params- 业务参数(已过滤空值)secret- 密钥timestamp- 时间戳(秒)requestPath- 请求路径nonce- 随机字符串- Returns:
- 签名字符串(十六进制)
-
verify
Description copied from interface:SignGenerator验证签名- Specified by:
verifyin interfaceSignGenerator- Parameters:
params- 请求参数sign- 签名值secret- 密钥- Returns:
- 是否验证通过
-
verify
public boolean verify(Map<String, String> params, String sign, String secret, String timestamp, String requestPath, String nonce) Description copied from interface:SignGenerator验证签名(完整参数)- Specified by:
verifyin interfaceSignGenerator- Parameters:
params- 请求参数sign- 签名值secret- 密钥timestamp- 时间戳(秒)requestPath- 请求路径nonce- 随机字符串- Returns:
- 是否验证通过
-
sign
Description copied from interface:SignGenerator使用密钥对象签名(非对称算法专用)对称算法(HMAC)不需要密钥对象,默认抛
UnsupportedOperationException; 非对称算法(SM2/RSA)应覆盖此方法接收私钥。- Specified by:
signin interfaceSignGenerator- Parameters:
content- 待签名内容(已规范化)privateKey- 私钥- Returns:
- 签名值(Base64 或 Hex 字符串,由实现决定)
-
verify
Description copied from interface:SignGenerator使用密钥对象验签(非对称算法专用)对称算法(HMAC)不需要密钥对象,默认抛
UnsupportedOperationException; 非对称算法(SM2/RSA)应覆盖此方法接收公钥。- Specified by:
verifyin interfaceSignGenerator- Parameters:
content- 待验签内容(已规范化)signature- 签名值publicKey- 公钥- Returns:
- 是否验证通过
-
getAlgorithm
Description copied from interface:SignGenerator获取支持的算法- Specified by:
getAlgorithmin interfaceSignGenerator- Returns:
- ical算法
-