来自Google脚本的二进制输出HMAC encription [英] Binary Output from Google Script HMAC encription

查看:253
本文介绍了来自Google脚本的二进制输出HMAC encription的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Google Apps脚本,并试图编写&向AWS CloudWatch签署HTTP请求。

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

在Amazon API文档 here 有关如何创建签名密钥,他们使用伪来解释HMAC算法将返回二进制格式。

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应用脚本提供了一种做这种散列的方法,

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[]

如何转换AWS想要的二进制数据Byte []?或者是否有我可以在Google Apps脚本中使用的vanilla javascript函数来计算哈希?

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

推荐答案

从字节数组到所需的二进制数据的转换应该很简单:

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);

但是您必须查看这个在bugtracker中的公开问题 - 转换中可能存在一些问题。

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

也许你可以尝试创建一个String.fromCharCode()循环并避免负数:

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);

这篇关于来自Google脚本的二进制输出HMAC encription的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆