goog.cryp.Hmac无法从节点js的crypt再现结果
我正在尝试重现here中描述的HMAC签名。
该示例使用的是crypt
中的nodejs
,但就我而言,我需要使用Google闭包库。因此,我尝试使用goog.crypt.Hmac
库来重现HMAC签名。
下面是我的实验代码。
let crypto = require('crypto');
require("google-closure-library");
goog.require("goog.crypt.Hmac");
goog.require("goog.crypt");
goog.require('goog.crypt.Sha1');
function forceUnicodeEncoding(string) {
return decodeURIComponent(encodeURIComponent(string));
}
function nodejs_crypto(string_to_sign, secret) {
signature = crypto.createHmac('sha1', secret)
.update(forceUnicodeEncoding(string_to_sign))
.digest('base64')
.trim();
return signature
}
function goog_crypto(string_to_sign, secret) {
const hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), goog.crypt.stringToByteArray(secret));
const hash = hmac.getHmac(forceUnicodeEncoding(string_to_sign));
return hash.toString()
}
const string_to_sign = "message";
const secret = "secret";
const sig1 = nodejs_crypto(string_to_sign, secret);
const sig2 = goog_crypto(string_to_sign, secret);
console.log(sig1);
// DK9kn+7klT2Hv5A6wRdsReAo3xY=
console.log(sig2);
// 12,175,100,159,238,228,149,61,135,191,144,58,193,23,108,69,224,40,223,22
回答如下:- 您的goog_crypto函数看起来正确,因为结果相同,尽管以base64与整数(字节)数组表示)
getHmac
的返回值,即您代码中的hash
是一个整数数组-如documented here ...array.toString()
与array.join()
几乎相同- 如果您想让goog_crypto函数返回base64,请使用nodejs Buffer toString为您完成艰苦的工作
即
function goog_crypto(string_to_sign, secret) {
const hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), goog.crypt.stringToByteArray(secret));
const hash = hmac.getHmac(forceUnicodeEncoding(string_to_sign));
return Buffer.from(hash).toString('base64');
}
由于hash
是Numbers
的数组,所以Buffer.from(hash)
从hash
中的数字创建一个缓冲区-buffer.toString('base64')
返回base64中的缓冲区数据[[encoded
关于第1点:证明您在代码中获得的结果相同
const sig1 = "DK9kn+7klT2Hv5A6wRdsReAo3xY=";
const sig2 = '12,175,100,159,238,228,149,61,135,191,144,58,193,23,108,69,224,40,223,22';
const sig1AsNumArrayString = atob(sig1).split('').map(c => c.charCodeAt(0)).toString();
console.log(sig2 === sig1AsNumArrayString)
goog.cryp.Hmac无法从节点js的crypt再现结果
我正在尝试重现here中描述的HMAC签名。
该示例使用的是crypt
中的nodejs
,但就我而言,我需要使用Google闭包库。因此,我尝试使用goog.crypt.Hmac
库来重现HMAC签名。
下面是我的实验代码。
let crypto = require('crypto');
require("google-closure-library");
goog.require("goog.crypt.Hmac");
goog.require("goog.crypt");
goog.require('goog.crypt.Sha1');
function forceUnicodeEncoding(string) {
return decodeURIComponent(encodeURIComponent(string));
}
function nodejs_crypto(string_to_sign, secret) {
signature = crypto.createHmac('sha1', secret)
.update(forceUnicodeEncoding(string_to_sign))
.digest('base64')
.trim();
return signature
}
function goog_crypto(string_to_sign, secret) {
const hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), goog.crypt.stringToByteArray(secret));
const hash = hmac.getHmac(forceUnicodeEncoding(string_to_sign));
return hash.toString()
}
const string_to_sign = "message";
const secret = "secret";
const sig1 = nodejs_crypto(string_to_sign, secret);
const sig2 = goog_crypto(string_to_sign, secret);
console.log(sig1);
// DK9kn+7klT2Hv5A6wRdsReAo3xY=
console.log(sig2);
// 12,175,100,159,238,228,149,61,135,191,144,58,193,23,108,69,224,40,223,22
回答如下:- 您的goog_crypto函数看起来正确,因为结果相同,尽管以base64与整数(字节)数组表示)
getHmac
的返回值,即您代码中的hash
是一个整数数组-如documented here ...array.toString()
与array.join()
几乎相同- 如果您想让goog_crypto函数返回base64,请使用nodejs Buffer toString为您完成艰苦的工作
即
function goog_crypto(string_to_sign, secret) {
const hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), goog.crypt.stringToByteArray(secret));
const hash = hmac.getHmac(forceUnicodeEncoding(string_to_sign));
return Buffer.from(hash).toString('base64');
}
由于hash
是Numbers
的数组,所以Buffer.from(hash)
从hash
中的数字创建一个缓冲区-buffer.toString('base64')
返回base64中的缓冲区数据[[encoded
关于第1点:证明您在代码中获得的结果相同
const sig1 = "DK9kn+7klT2Hv5A6wRdsReAo3xY=";
const sig2 = '12,175,100,159,238,228,149,61,135,191,144,58,193,23,108,69,224,40,223,22';
const sig1AsNumArrayString = atob(sig1).split('').map(c => c.charCodeAt(0)).toString();
console.log(sig2 === sig1AsNumArrayString)
与本文相关的文章
评论列表 (0)
- 暂无评论