Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么node.js SHA1 body与crom GitHub webhook SHA1的值不同?_Node.js_Github_Sha1 - Fatal编程技术网

为什么node.js SHA1 body与crom GitHub webhook SHA1的值不同?

为什么node.js SHA1 body与crom GitHub webhook SHA1的值不同?,node.js,github,sha1,Node.js,Github,Sha1,我已经多次尝试使用node.jscrypto包来计算req.bodySHA1值。但总是不同的 我正在使用以下代码: var body = req.body; var sig = 'sha1=' + crypto.createHmac('sha1', secret).update(JSON.stringify(body)).digest('hex'); console.log(sig === req.headers['x-hub-signature']) // not equal :( 或 如果

我已经多次尝试使用node.js
crypto
包来计算
req.body
SHA1值。但总是不同的

我正在使用以下代码:

var body = req.body;
var sig = 'sha1=' + crypto.createHmac('sha1', secret).update(JSON.stringify(body)).digest('hex');
console.log(sig === req.headers['x-hub-signature']) // not equal :(

如果你能给我任何帮助,我将不胜感激


谢谢:)

最后,我知道引起这个问题的原因。 因为,Github向我的服务器发布一个字符串,Github将该字符串的值设为SHA1。但是对于我的nodejs代码,我使用
body parser
包作为中间件,所以我得到了

{  
  payload: {...}  
}  
而SHA1,所以,从来没有平等的

现在,我们知道了原因,我们可以解决这个问题。只是

  var bufferArr = [];
  var bufferLen = 0;
  req.on('data', function(chunk) {
    bufferArr.push(chunk);
    bufferLen += chunk.length;
  });
  req.on('end', function() {
    var data = Buffer.concat(bufferArr, bufferLen).toString();
    var sig = 'sha1=' + crypto.createHmac('sha1', secret)
        .update(data).digest('hex');
    console.log(sig === req.headers['x-hub-signature']) // true
  }
我希望我的经验能帮助别人

  var bufferArr = [];
  var bufferLen = 0;
  req.on('data', function(chunk) {
    bufferArr.push(chunk);
    bufferLen += chunk.length;
  });
  req.on('end', function() {
    var data = Buffer.concat(bufferArr, bufferLen).toString();
    var sig = 'sha1=' + crypto.createHmac('sha1', secret)
        .update(data).digest('hex');
    console.log(sig === req.headers['x-hub-signature']) // true
  }