简体   繁体   English

PHP 和 Go 中的 hmac 哈希不匹配

[英]hmac hash mismatch in PHP and Go

I am trying to connect to an API that uses an outdated hmac hash authentication mechanism for the API's.我正在尝试连接到一个 API,该 API 对 API 使用过时的 hmac 哈希身份验证机制。

For an instance:例如:

$signature = hash_hmac('sha256', $string_to_sign, $api_sec);

vs the one generated in Go:与 Go 中生成的相比:

h := hmac.New(sha256.New, []byte(authSecret))
h.Write([]byte(stringToSign))
signature := hex.EncodeToString(h.Sum(nil))

When I use the same stringToSign($string_to_sign) and same authSecret($api_sec) the signature generated with Go results as an invalid signature for the API.当我使用相同的stringToSign($string_to_sign)和相同的authSecret($api_sec) ,使用 Go 生成的签名结果为 API 的无效签名。 But if I create the same with the PHP function it works fine.但是如果我用 PHP 函数创建相同的函数,它就可以正常工作。 I am a bit lost as to where to look.我有点不知道在哪里看。

There must be an issue with your input data.您的输入数据肯定有问题。

Using the below PHP:使用以下 PHP:

echo hash_hmac('sha256', 'data', 'key');

And the below Go:和下面的去:

h := hmac.New(sha256.New, []byte("key"))
h.Write([]byte("data"))
signature := hex.EncodeToString(h.Sum(nil))
fmt.Println(signature)

I get the same result of 5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0我得到相同的结果5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM