相当于PHP的hash_hmac()和RAW BINARY输出的Google Apps脚本?
问题描述:
我必须使用hash_hmac连接到API。在hash_hmac的PHP文档中,第四个参数bool $ raw_output控制输出是原始二进制数据(true)还是小写hexits(false)。只需将该参数设置为true,我的程序就可以在PHP中使用。相当于PHP的hash_hmac()和RAW BINARY输出的Google Apps脚本?
这是PHP是什么在起作用:(?或可我)
$signature = base64_encode(hash_hmac('SHA256', $signature_string, $private_key, true))
在谷歌企业应用套件我不能使用任何JavaScript库,但有这样的功能:Utilities.computeHmacSha256Signature https://developers.google.com/apps-script/reference/utilities/utilities#computehmacsha256signaturevalue-key
然而这没有PHP的“真实”选项,所以它不输出原始二进制数据。
如何使用Google Apps获得与我在PHP中相同的价值?
这是我在谷歌企业应用套件,但显然它不是做输出的原始二进制数据:
var signature = Utilities.computeHmacSha256Signature(signature_string, private_key);
我没有找到一个方法来为十六进制的响应(PHP相当于FALSE转换,而不是TRUE,但是这并不是解决方案带给我任何接近
// convert to hex
var signature_in_hex = signature.reduce(function(str,chr) {
chr = (chr < 0 ? chr + 256 : chr).toString(16);
return str + (chr.length==1?'0':'') + chr;
},'');
答
什么工作对我来说只是复制这些脚本直接进入谷歌企业应用套件:
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha256.js https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js
然后用:
var hash = CryptoJS.HmacSHA256(signature, private_key);
var base64 = hash.toString(CryptoJS.enc.Base64);
也许从发布PHP和GAppScript常见的输出将帮助我们找出问题? – Sammitch
我只是猜测在这里,但确实提供'Utilities.computeHmacSha256Signature()'生成的字节数组到'Utilities.newBlob()'做你需要的吗? –
虽然,看着https://stackoverflow.com/questions/17160896/binary-output-from-google-script-hmac-encription?rq=1会建议它不是解决方案... –