Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am current working with Google Apps script and am attempting to write & sign an HTTP request to AWS CloudWatch.

On the Amazon API documentation here regarding how to create a signing key, they use pseudo to explain that the HMAC algorithm is to return binary format.

HMAC(key, data) represents an HMAC-SHA256 function 
that returns output in binary format.

Google apps script offers a method to do such a hash,

Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,
                                            data,
                                            key);

but the return type is always a byte array.

Byte[]

How do I convert the Byte[] to the binary data AWS wants? Or is there a vanilla javascript function I can use in Google Apps Script to compute the hash?

Thanks

share|improve this question
1  
Have you tried converting to String first (from Byte[]) and then, converting it to Binary would be easier? –  Rpranata Jun 18 '13 at 5:18
    
Yes I, have tried that by converting the Byte[] to a blob and then reading the data as string to be passed into the next iteration of hashing, but to no avail –  user2495753 Jun 18 '13 at 6:01

1 Answer 1

up vote 1 down vote accepted

The conversion from byte array to the binary data required should be simple:

kDate = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,
             '20130618', 'AWS4' + kSecret);
kDate = Utilities.newBlob(kDate).getDataAsString();
kRegion = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, 
             'eu-west-1', kDate);

BUT you have to look onto this open issue in the bugtracker - there could be some issues in conversion.

maybe you could try to make a String.fromCharCode() loop and avoid negative numers:

kDateB = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,
             '20130618', 'AWS4' + kSecret);
kDate = '';
for (var i=0; i<kDateB.length; i++)
  kDate += String.fromCharCode(kDateB[i]<0?256+kDateB[i]:0+kDateB[i]);
kRegion = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, 
             'eu-west-1', kDate);
share|improve this answer
    
Thank you for the reply. This is definitely frustrating. I made a comment on the bugtracker, so hopefully this will get the google team to at least respond and give an update to this known issue. –  user2495753 Jun 18 '13 at 8:30
    
did you try the ´String.fromCharCode` option? The problem seems to appear with negative Byte values. Adding 256 to these should solve the problem. –  Taras Jun 18 '13 at 8:54
    
I did try to do this and get rid of negative values by adding 256, but unfortunately I am still getting the incorrect result. Google has said that this is a known issue and is working on it, but they have also stated that there is no update to this bug at the current time. –  user2495753 Jun 20 '13 at 2:20
    
I managed to get the signature to work, but only by using CryptoJS which I imported into Google Script by copy and pasting the raw javascript code, but I still haven't managed to get pure Google code to work. –  user2495753 Jun 20 '13 at 5:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.