我正在尝试连接到 API,该 API 对 API 使用过时的 hmac 哈希身份验证机制。
举个例子:
$signature = hash_hmac('sha256', $string_to_sign, $api_sec);
与 Go 中生成的相比:
h := hmac.New(sha256.New, []byte(authSecret))
h.Write([]byte(stringToSign))
signature := hex.EncodeToString(h.Sum(nil))
当我使用相同的 stringToSign($string_to_sign)
和相同的 authSecret($api_sec)
时,Go 生成的签名结果是 API 的无效签名。但是,如果我使用 PHP 函数创建相同的内容,它就可以正常工作。我对去哪里看有点迷茫。
最佳答案
一定是您的输入数据有问题。
使用以下 PHP:
echo hash_hmac('sha256', 'data', 'key');
下面的 Go:
h := hmac.New(sha256.New, []byte("key"))
h.Write([]byte("data"))
signature := hex.EncodeToString(h.Sum(nil))
fmt.Println(signature)
我得到相同的结果 5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0
关于php - PHP 和 Go 中的 hmac 哈希不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737926/