为什么我无法计算正确的HMAC签名? [英] Why can't I compute a correct HMAC signature?

查看:86
本文介绍了为什么我无法计算正确的HMAC签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算Google Apps Script中的HMAC签名,但文档并未完全清楚我需要如何传递参数,而且我一直无法获得预期的输出结果。






为了确定我是否得到正确的输出,我将结果与已知的良好PHP代码进行了比较。该代码是:

  $ key =a2V5; #这是keybase64编码
$ value =test;
$ result = base64_encode(hash_hmac('sha512',$ value,base64_decode($ key),true));






我在Google Apps Script中的代码是:

  key =a2V5; //这是keybase64编码
value =test;
result = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,value,Utilities.base64Decode(key)));

我预期收到的结果是:

  KHoPuJp / vfpbVThjaRjlN6W4MGXk / zMSaLeqoRXd4EepsPT7W4KGCPwLYyfxAFX3Y3sFjp4Nu55piQGj5t1GHA == 

取而代之的是:

  mGXJ3X / nH5ZIFUAPtf1PsViY50pD3cfU7J8w2KAIEAqrAgZ3dpKcuy5V1yvH4 / C5n1C9rFFsKc2JKHTwUqPscQ == 

我在这里弄到了什么?

解决方案

有一件事引起了我的注意:
$ b $ p $ Utilities.base64Decode(key)方法返回 Byte []
Utilities.computeHmacSignature(macAlgorithm,value,key)接受3个参数。 类型为字符串



也许这是问题所在。为什么不尝试类似下面的方法,然后检查结果:

  key =a2V5; //这是keybase64编码的
clearKey =key;
value =test;
result = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,value,clearKey));

我检查Google Apps脚本这里


I am trying to compute an HMAC signature in Google Apps Script, but the documentation isn't 100% clear on how I need to pass in the parameters, and I have been unable to get the expected output.


To determine if I am getting correct output, I am comparing the result against known-good PHP code. That code is:

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


My code in Google Apps Script is:

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

The output I expected to receive was:

KHoPuJp/vfpbVThjaRjlN6W4MGXk/zMSaLeqoRXd4EepsPT7W4KGCPwLYyfxAFX3Y3sFjp4Nu55piQGj5t1GHA==

But what I got instead was:

mGXJ3X/nH5ZIFUAPtf1PsViY50pD3cfU7J8w2KAIEAqrAgZ3dpKcuy5V1yvH4/C5n1C9rFFsKc2JKHTwUqPscQ==

What did I screw up here?

解决方案

I reviewed your code and there is one thing which caught my eye:

Utilities.base64Decode(key) method returns Byte[] Utilities.computeHmacSignature(macAlgorithm, value, key) accepts 3 parameters. value and key are of type string.

Maybe this is the issue. Why don't you try something like the following and check results then:

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

I check Google Apps Script here.

这篇关于为什么我无法计算正确的HMAC签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆