Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I compute a correct HMAC signature?

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?

like image 734
Michael Hampton Avatar asked Dec 22 '12 22:12

Michael Hampton


1 Answers

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.

like image 72
Tom Avatar answered Nov 02 '22 09:11

Tom