我正在尝试在 Google Apps 脚本中计算 HMAC 签名,但文档并未 100% 清楚地说明我需要如何传递参数,并且我无法获得预期的输出。
为了确定我是否获得正确的输出,我将结果与已知良好的 PHP 代码进行比较。该代码是:
$key = "a2V5"; # this is "key" base64-encoded
$value = "test";
$result = base64_encode(hash_hmac('sha512', $value, base64_decode($key), true));
我在 Google 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==
我在这里搞砸了什么?
我查看了您的代码,有一件事引起了我的注意:
Utilities.base64Decode(key)
方法返回Byte[]
Utilities.computeHmacSignature(macAlgorithm, value, key)
接受 3 个参数。value
and key
属于类型string
.
也许这就是问题所在。为什么不尝试以下方法并检查结果:
key = "a2V5"; // this is "key" base64-encoded
clearKey = "key";
value = "test";
result = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, value, clearKey));
我检查 Google Apps 脚本here https://developers.google.com/apps-script/class_utilities?hl=en.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)