PHP和Go中的hmac哈希不匹配

PHP和Go中的hmac哈希不匹配

问题描述:

我正在尝试连接到对API使用过时的hmac哈希身份验证机制的API.

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

对于实例:

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

vs Go中生成的一个:

vs the one generated in 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函数创建相同的控件,则效果很好.我对去哪里看有点迷茫.

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.

您的输入数据一定存在问题.

There must be an issue with your input data.

使用以下PHP:

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

然后执行以下操作:

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

我得到与 5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0