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 returnsByte[]
Utilities.computeHmacSignature(macAlgorithm, value, key)
accepts 3 parameters.value
andkey
are of typestring
.Maybe this is the issue. Why don't you try something like the following and check results then:
I check Google Apps Script here.