Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 为什么可以';我不能计算一个正确的HMAC签名吗?_Google Apps Script - Fatal编程技术网

Google apps script 为什么可以';我不能计算一个正确的HMAC签名吗?

Google apps script 为什么可以';我不能计算一个正确的HMAC签名吗?,google-apps-script,Google Apps Script,我试图在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脚本中计算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.base64解码(键)
方法返回
字节[]
Utilities.computeHmacSignature(macAlgorithm,value,key)
接受3个参数<代码>值和
属于
字符串
类型

也许这就是问题所在。为什么不尝试以下方法并检查结果:

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

我检查谷歌应用程序脚本。

这会给出正确的结果。虽然这意味着我必须报告API中的一个错误,这是另一回事……如果你需要接受base64编码的输入,你可以使用
String.fromCharCode
将字节数组输出循环到它上面,将其重新构建成字符串。哦,这伤了我的头。我可能会这么做,但如果谷歌第一次能把事情做好,那就太好了……为了将来的参考,我向谷歌提交了一份报告,并最终在项目中使用了它,而不是谷歌的破
实用程序。computeHmacSignature
方法。米阿切尔对jsSHA的建议非常有效。我要补充一点,在谷歌脚本中使用库是违反直觉的。只需将
src/sha256.js
粘贴到脚本中的新文件中,直接使用
jsSHA
,无需
require
,也无需使用谷歌的库界面。此外,错误报告仍然没有解决。为了将来的参考,发布谷歌应用程序脚本。由于现在的更新,我有一些HMAC与PHP匹配,有些不匹配。