为什么我不能计算出正确的HMAC签名?(Why can't I compute a corr

2019-08-04 13:16发布

我试图计算在谷歌Apps脚本的HMAC签名,但文档是如何,我需要在参数传递不是100%清楚,我一直无法得到预期的输出。


如果要判断我得到正确的输出,我比较反对已知良好的PHP代码的结果。 这代码是:

$key = "a2V5"; # this is "key" base64-encoded
$value = "test";
$result = base64_encode(hash_hmac('sha512', $value, base64_decode($key), true));

我在谷歌Apps脚本代码是:

key = "a2V5"; // this is "key" base64-encoded
value = "test";
result = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, value, Utilities.base64Decode(key)));

我希望得到的输出是:

KHoPuJp/vfpbVThjaRjlN6W4MGXk/zMSaLeqoRXd4EepsPT7W4KGCPwLYyfxAFX3Y3sFjp4Nu55piQGj5t1GHA==

但我得到的却是:

mGXJ3X/nH5ZIFUAPtf1PsViY50pD3cfU7J8w2KAIEAqrAgZ3dpKcuy5V1yvH4/C5n1C9rFFsKc2JKHTwUqPscQ==

我是怎么搞砸了这里?

Answer 1:

我检查了您的代码,并有一两件事引起了我的眼睛:

Utilities.base64Decode(key)方法返回Byte[] Utilities.computeHmacSignature(macAlgorithm, value, key)接受3个参数。 valuekey的类型的string

也许这就是问题。 你为什么不尝试像那么下面,检查结果:

key = "a2V5"; // this is "key" base64-encoded
clearKey = "key";
value = "test";
result = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, value, clearKey));

我检查谷歌Apps脚本在这里 。



文章来源: Why can't I compute a correct HMAC signature?