crypto
我正在尝试在不支持标准节点库的设备上生成 SHA256 和 HmacSHA512 哈希。所以我正在调整代码以使用 CryptoJS。但是,CryptoJS 无法将哈希编码为二进制(只有 Hex、Base64 和 Latin1 是可用的编码器)。
以下是我要迁移的功能。以前的(不可用的)代码被注释掉了。
const getMessageSignature = (path, request, secret, nonce) => {
// Expected outcome:
// API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
const message = JSON.stringify(request);
const secret_buffer = btoa(secret);
const hash = CryptoJS.algo.SHA256.create();
const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret_buffer);
const hash_digest = hash.update(nonce + message).finalize().toString(CryptoJS.enc.Base64);
const hmac_digest = hmac.update(path + hash_digest).finalize().toString(CryptoJS.enc.Base64);
// CANNOT USE BELOW (Buffer and crypto not supported)
// const secret_buffer = new Buffer(secret, 'base64');
// const hash = new crypto.createHash('sha256');
// const hmac = new crypto.createHmac('sha512', secret_buffer);
// const hash_digest = hash.update(nonce + message).digest('binary');
// const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
};