PHP 和 Go 中的 hmac 哈希不匹配
hmac hash mismatch in PHP and Go
我正在尝试连接到 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
相同的结果
我正在尝试连接到 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