0

I have been battling with the following for a while and thought to ask help.

I am trying to compute a signature of a string using HMAC-SHA-256 algorithm in Apps Script.

And trying to reproduce the example with input and key example provided here

var input_string = "1KAD46OrT9HafiKdsXeg1588925778000"
var key_string = "4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC" 

var test=Utilities.base64EncodeWebSafe(
  Utilities.computeHmacSha256Signature(
    input_string,
    key_string
  )
);

The above returns:

zqr7XM3C9yOp_T6R09IjjuDdmm18PDZd61D8KvJ3qoM=

Instead of (as show in signature example or from using this online tool):

ceaafb5ccdc2f723a9fd3e91d3d2238ee0dd9a6d7c3c365deb50fc2af277aa83

Any idea of what I am doing wrong ?

Thank you !!

Leonard
  • 13
  • 2

1 Answers1

1

base64EncodeWebSafe(data) accepts a string data to encode. While computeHmacSha256Signature(value, key) returns a byte array representing the output signature.

You need to convert Byte array to hex string.

function test(){
  var input_string = "1KAD46OrT9HafiKdsXeg1588925778000"
  var key_string = "4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC" 
  var test = Utilities.computeHmacSha256Signature(input_string,key_string).reduce(function(str,chr){
    chr = (chr < 0 ? chr + 256 : chr).toString(16);
    return str + (chr.length==1?'0':'') + chr;
  },'');;

  Logger.log(test);
}

OUTPUT: ceaafb5ccdc2f723a9fd3e91d3d2238ee0dd9a6d7c3c365deb50fc2af277aa83

Ron M
  • 5,791
  • 1
  • 4
  • 16