hmac hash mismatch in PHP and Go

巧了我就是萌 提交于 2021-02-05 07:17:25

问题


I am trying to connect to an API that uses an outdated hmac hash authentication mechanism for the API's.

For an instance:

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

vs the one generated in 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. But if I create the same with the PHP function it works fine. I am a bit lost as to where to look.


回答1:


There must be an issue with your input data.

Using the below 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



来源:https://stackoverflow.com/questions/54737926/hmac-hash-mismatch-in-php-and-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!