Class HmacSignGenerator
java.lang.Object
tech.riemann.nif.web.sign.HmacSignGenerator
- All Implemented Interfaces:
SignGenerator
HMAC-SHA256签名生成器
签名内容组成(按字典序排序后连接): 1. 请求参数的MD5摘要(paramsMd5) 2. 时间戳(timestamp) 3. 请求路径(requestPath) 4. 随机字符串(nonce)
M6: 应用密钥(appSecret)不再拼入被签名消息,仅作为 HMAC key 使用
签名流程: 1. 计算请求参数的MD5摘要 2. 将 paramsMd5、timestamp、requestPath、nonce 按字典序排序 3. 连接成字符串:key1=value1&key2=value2 4. 计算HMAC-SHA256哈希值(appSecret 作为 HMAC key) 5. 转换为十六进制字符串
- 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) 生成签名(完整参数)获取支持的算法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, sign, verify
-
Constructor Details
-
HmacSignGenerator
public HmacSignGenerator()
-
-
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
-
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) 验证签名(完整参数)- Specified by:
verifyin interfaceSignGenerator- Parameters:
params- 请求参数sign- 签名值secret- 应用密钥timestamp- 时间戳(秒)requestPath- 请求路径nonce- 随机字符串- Returns:
- 是否验证通过
-
getAlgorithm
Description copied from interface:SignGenerator获取支持的算法- Specified by:
getAlgorithmin interfaceSignGenerator- Returns:
- ical算法
-