Binary Output from Google Script HMAC Designation

I am currently working with a Google Apps script and am trying to write and sign an HTTP request to AWS CloudWatch.

In the Amazon API documentation here on how to create a signature key, they use pseudo to explain that the HMAC algorithm should return a binary format.

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

Google application script offers a method to execute 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 Byte [] to AWS binary data? Or is there a javascript function in vanilla that I can use in a Google Apps script to calculate the hash?

thanks

+6
source share
2 answers

Converting from a byte array to the required binary data 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 should look at this open problem in bugtracker - there may be some problems in the conversion.

perhaps you could try creating a String.fromCharCode () loop and avoid negative numbers:

 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); 
+1
source

I am pretty sure that this is an error that Utilities.computeHmacSignature occupies the ASCII key. But there was no way to parse byte [] in ASCII in GAS correctly

And the library writer is too stupid, just provide a function that takes the key as a byte []

Therefore, I use this instead: http://caligatio.github.com/jsSHA/

Just copy SHA.js and SHA-256.js, then it works fine

PS. it spends my time for the whole 2 days, so I'm very annoying

+2
source

Source: https://habr.com/ru/post/947519/


All Articles