使用AK与SK组建密钥
该文章距离发布日期已经过了 1169 天 ,请注意信息甄别。
AccessKey
md5($uniqid+time())
SecretKey
sha1($uniqid+time())
校验方式
HMAC-SHA1
HMAC 是哈希运算消息认证码 (Hash-based Message Authentication Code),HMAC 运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。您可以使用它同时验证数据的完整性和消息的真实性。HMAC-SHA1 签名算法是一种常用的签名算法,用于对一段信息进行生成签名摘要。
使用hmac_sha1
方式加盐,PHP 内置函数hash_mac('sha1',$data,$key)
//JAVA实现
public static String hmacSha1(String value, String key) {
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(value.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
return new String(hexBytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
接口设计
{
"uniqid":"5ed75482bdf5a",
"timestamp":"",
"oid":"20200604063905959098",
"amount":"121",
"currency":"HKD",
"type":"wechatpay",
"info":"product info",
"client_ip":"8.8.8.8",
"callback_url":"http://test.test/notify",
"signature":""
PHP HELPER
URL 安全的 Base64 编码
URL 安全的 Base64 编码适用于以 URL 方式传递 Base64 编码结果的场景。该编码方式的基本过程是先将内容以 Base64 格式编码为字符串,然后检查该结果字符串,将字符串中的加号
+
换成中划线-
,并且将斜杠/
换成下划线_
。
function base64_url_encode($input) {
return strtr(base64_encode($input), '+/', '._');
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '._-', '+/'));
}