php - PHP 和 Go 中的 hmac 哈希不匹配

标签 php go sha256 hmac

我正在尝试连接到 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/

相关文章:

php - 本地主机中的 XAMPP 延迟

java - 简单的 AES 在 PHP 中加密,在 Java 中解密

linux - Golang for-select 炸毁 CPU

go - 在这个特定示例中,我应该在哪里关闭 channel ?

math - 与截断的 SHA-256 哈希发生冲突的概率

php - PHP 中的 Vungle 回调

php - 如何从php中的文件夹中删除文件

php - 嵌套的 php 三元问题 : ternary output ! = if - else

linux - 捕获中断时抑制终端输出中的 '^C' 字符串

lua - 如何在 Lua 中使用 HMAC - Lightroom 插件