微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Apps脚本相当于节点缓冲区对象是什么? 示例脚本:其他模式:参考文献:

如何解决Google Apps脚本相当于节点缓冲区对象是什么? 示例脚本:其他模式:参考文献:

Node.js Fiddle

const crypto = require('crypto');

let secret = 'my_secret';
let message = 'my_message';

let signer = crypto.createHmac('sha512',secret)
const signature = signer.update(message).digest('base64');
console.log(signature); 
//signature = DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==

// ***** Use Hex Buffer instead of string - same result
let message_buffer = Buffer.from(message); //<Buffer 6d 79 5f 6d 65 73 73 61 67 65>
let signer_from_buffer = crypto.createHmac('sha512',secret)
const signature_from_buffer = signer_from_buffer.update(message_buffer).digest('base64');
console.log(signature_from_buffer); 
// signature = DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==

以及在Google Apps脚本中:

var secret =  'my_secret';
var message = 'my_message';

var signature_hash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,message,secret);
var signature = Utilities.base64Encode(signature_hash);
Logger.log(signature);
//signature = DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==

// Use Hex Array instead of string - different result
var message_buffer = ["6d","79","5f","6d","65","73","61","67","65"];
var signature_hash_from_buffer = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,message_buffer,secret);
var signature_from_buffer = Utilities.base64Encode(signature_hash_from_buffer );
Logger.log(signature_from_buffer );
//signature_from_buffer = gGK0Y/KytE+8ZKWs/og1VQ1wMdpnoFmJMCHGpKdi+QODFwykqvDK5qJwgzZrr1b1g5050j9r8jpfXlM2ZA+3qQ==

所以我知道我的加密过程正在正常运行。问题是,我从十六进制数组开始,所以我希望能够获得相同的结果。我不知道节点缓冲区是哪种对象,以及如何将其转换为Google Apps脚本。

解决方法

我相信您的目标如下。

  • 您要使用Google Apps脚本从DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==中检索["6d","79","5f","6d","65","73","61","67","65"]

在Google Apps脚本中,getBytes()返回Int8Array,它是一个二进制补码8位有符号整数的数组。因此,在这种情况下,首先需要将["6d","65"]转换为Int8Array。并且当字节数组用于Utilities.computeHmacSignature时,还需要将secret转换为字节数组。关于这一点,TheMaster的回答已经提到了它。 Ref

以上几点都会反映到Google Apps脚本中,如下所示。

示例脚本:

function myFunction() {
  var secret =  'my_secret';
  var message_buffer = ["6d","65"];
  
  // Convert message_buffer (Unsigned hexadecimal array) to Int8Array.
  var message = message_buffer.map(e => parseInt(e[0],16).toString(2).length == 4 ? parseInt(e,16) - 256: parseInt(e,16));
  
  var signature_hash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,message,Utilities.newBlob(secret).getBytes());
  var signature = Utilities.base64Encode(signature_hash);
  Logger.log(signature);
  // DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==
}

其他模式:

与其他模式一样,在此模式中,message_buffer转换为Unit8Array,Unit8Array转换为Int8Array,然后将Int8Array与Utilities.computeHmacSignature()一起使用。

function myFunction() {
  var secret =  'my_secret';
  var message_buffer = ["6d","65"];
  
  // Convert message_buffer (Unsigned hexadecimal array) to Unit8Array.
  var unit8Array = message_buffer.map(e => parseInt(e,16));
  
  // Convert Unit8Array to Int8Array.
  var int8Array = [...new Int8Array(Uint8Array.from(unit8Array).buffer)];

  var signature_hash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,int8Array,Utilities.newBlob(secret).getBytes());
  var signature = Utilities.base64Encode(signature_hash);
  Logger.log(signature);
  // DWpafZMnI4PT5v0jdidFtU5qoh3fsvUKnaOga/Y2Nzy/tvsx1F9p61SjE+hlRQ97y/LMmBkG39IyL5Ja46bJlw==
}

参考文献:

,

node documentation所述,

缓冲区对象用于表示固定长度的字节序列。许多Node.js API支持缓冲区。

Buffer类是JavaScript的Uint8Array类的子类,并使用涵盖其他用例的方法对其进行了扩展。只要支持缓冲区,Node.js API都可以接受普通的Uint8Arrays。

Buffer没有直接支持

  • Apps脚本支持Byte Array from Blobs。这样就可以模拟Buffer.from

  • 在Google Apps脚本中,Utilities.newBlob(str).getBytes()返回Int8Array。为了将无符号十六进制数组转换为Google Apps脚本的字节数组,需要将其转换为Int8Array

  • Apps脚本也直接支持Uint8Array

const Buffer={from: str => Utilities.newBlob(str).getBytes()};
var message_buffer = Buffer.from(message);
var signature_hash_from_buffer = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,message_buffer,Buffer.from(secret));//modified as well

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。