无法验证签名

时间:2019-03-19 13:59:52

标签: javascript node.js encryption

我正在尝试为生成的哈希创建签名。在我将数据存储到数据库 verifySignature 之前,返回true,因此一切正常。在将签名存储到数据库中之前,我将其转换为JSON,因为我无法存储[Object object]

router.post('/', function(req, res, next) {

  var publicKey = '041e56b61d36fcef7942b671b1e9773bc105d7e5fb831f67d9004ddfb88b64cb522610d8efcebaf445794dc2f722fd53c1de92c3efe7fbb25ac9044715dd3632c8';
  var privateKey = '704bc2b2e8d3f49ea6535498a82c4d09287c5f53acb4ebf1e55aca7e520c3f77';

  var host = req.body.host;
  var value = req.body.value;
  var select = req.body.selectpicker;
  var conncated = host.concat(value,select);
  var message = Hash(conncated);
  var signature = createSignature(message,privateKey);

  var post  = {
    host: host,
    type: select,
    value: value,
    signature: JSON.stringify(signature),
    public : publicKey,
    hash: message
  };

  var query = db.query('INSERT INTO diploma SET ?', post, function(err, result) {

  });

  const isVerified = verifySignature(message, publicKey, signature);
  console.log('> Is verified: ', isVerified); //returns true

  function verifySignature(message, publicAddress, signature) {
    const messageHash = Hash(message);
    const publicKeyPair = ec.keyFromPublic(publicAddress, 'hex'); // use the accessible public key to verify the signature
    const isVerified = publicKeyPair.verify(messageHash, signature);
    return isVerified;
  }

  function createSignature(message, privateKey) {
    const messageHash = Hash(message);
    const privateKeyPair = ec.keyFromPrivate(privateKey);
    const signature = ec.sign(messageHash, privateKeyPair); // generate a signature on the hashed message with the private key
    return signature;
  }

});

但是,当我从db verifySignature 取回数据时,返回false。我已经对公共密钥和哈希进行了硬编码,以确保没有编码失配。

router.get('/', function(req, res, next) {
  var searchValue = req.query['hostname'];
  var query = db.query('SELECT value,signature,hash,public FROM diploma WHERE host =  ?', searchValue, function(err, result) {

    var public = '041e56b61d36fcef7942b671b1e9773bc105d7e5fb831f67d9004ddfb88b64cb522610d8efcebaf445794dc2f722fd53c1de92c3efe7fbb25ac9044715dd3632c8';
    var signature = JSON.parse(result[0].signature);
    var hash = '81faaa9f6b615cae3d7d8aa39f2c02ef93f3ae20';

    const isVerified = verify(hash,public,signature);
    console.log('> Is verified: ', isVerified); //returns false

    function verify(hash, publicAddress, signature) {
      const publicKeyPair = ec.keyFromPublic(publicAddress, 'hex'); // use the accessible public key to verify the signature
      const isVerified = publicKeyPair.verify(hash, signature);
      return isVerified;
    }

  });

mysql排序规则是否有问题,因为我的构想已耗尽。

1 个答案:

答案 0 :(得分:0)

I have no idea why but i had to chage verify function to following and its now working.

function verify(hash, publicAddress, signature) {
  const messageHash = Hash(hash);
  const publicKeyPair = ec.keyFromPublic(publicAddress, 'hex'); // use the accessible public key to verify the signature
  return publicKeyPair.verify(messageHash, JSON.parse(signature));
}