如何检查ECC Key和Public Key是否通过Js文件传递?

How can I check if ECC Key and Public Key are being passed through Js file?

我找不到我的问题所在。我使用 PubNub 进行实时消息传递,示例项目使用 ECC(椭圆曲线加密)加密消息传递。

除了发送消息外,文件中的所有内容(动画、频道状态等)都有效。每次我点击发送时,我都会收到这条消息:

这是我的聊天-app.js 文件:

(function() {

  if (typeof(user) === 'undefined') {
    return;
  } 

  var output = document.getElementById('output'), 
      input = document.getElementById('input'), 
      picture = document.getElementById('picture'),
      presence = document.getElementById('presence'),
      action = document.getElementById('action'),
      send = document.getElementById('send');

  var channel = 'fun';

  var keysCache = {};

  var pubnub = PUBNUB.init({
      subscribe_key: 'sub-c-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      publish_key: 'pub-c-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      uuid: user.name,
      auth_key: user.gtoken,
      ssl: true
  });


  function displayOutput(message) {
    if(!message) return;
    if(typeof(message.text) === 'undefined') return;

    var html = '';

    if ('userid' in message && message.userid in keysCache) {

      var signature = message.signature;

      delete message.signature;

      var result = ecc.verify(keysCache[message.userid].publicKey, signature, JSON.stringify(message));

      if(result) {
        html = '<p><img src="'+ keysCache[message.userid].picture +'" class="avatar"><strong>' +  keysCache[message.userid].name + '</strong><br><span>' + message.text + '</span></p>';
      } else {
        html = '<p><img src="images/troll.png" class="avatar"><strong></strong><br><em>A troll tried to spoof '+ keysCache[message.userid].name +' (but failed).</em></p>';
      } 

      output.innerHTML = html + output.innerHTML;

    } else {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', '/user/' + message.userid, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
          var res = JSON.parse(xhr.responseText);

          keysCache[message.userid] = {
            'publicKey': res.publicKey,
            'name': res.name,
            'artist': res.artist,
            'picture': res.picture,
            'id': res.id
          }
          displayOutput(message);
        }
      };
      xhr.send(null); 
    }
  }

  function getHistory() {
    pubnub.history({
      channel: channel,
      count: 30,
      callback: function(messages) {
        messages[0].forEach(function(m){ 
          displayOutput(m);
        });
      }
    });
  }

  pubnub.subscribe({
    channel: channel,
    restore: true,
    connect: getHistory,
    disconnect: function(res){
      console.log('disconnect called');
    },
    reconnect: function(res){
      console.log('reconnecting to pubnub');
    },
    callback: function(m) {
      displayOutput(m);
    },
    presence: function(m){
      if(m.occupancy === 1) {
        presence.textContent = m.occupancy + ' person online';
      } else {
        presence.textContent = m.occupancy + ' people online';
      }
      if((m.action === 'join') || (m.action === 'timeout') || (m.action === 'leave')){
        var status = (m.action === 'join') ? 'joined' : 'left';
        action.textContent = m.uuid + ' ' + status +' room';
        action.classList.add(m.action);
        action.classList.add('poof');
        action.addEventListener('animationend', function(){action.className='';}, false);
      }
    }
  });

  function post() {
    var safeText = input.value.replace(/\&/g, '&amp;').replace( /</g,  '&lt;').replace(/>/g,  '&gt;');
    var message = { text: safeText, userid: user.id };

    var signature = ecc.sign(user.eccKey, JSON.stringify(message));
    message['signature'] = signature;

    pubnub.publish({
      channel: channel,
      message: message
    });

    input.value = '';
  }

  input.addEventListener('keyup', function(e) {
    if(input.value === '') return;
    (e.keyCode || e.charCode) === 13 && post();
  }, false);

  send.addEventListener('click', function(e) {
    if(input.value === '') return;
    post();
  }, false);


})();

此错误来自我的 ecc.js 文件

Uncaught TypeError: Cannot read property 'r' of undefined 

但是,我假设错误出在 chat-app.js 文件中,因为我没有触及 ecc.js 文件。我从项目中得到它 -- https://github.com/pubnub/auth-chat-demo

有关 ECC 的更多信息可在此处找到:https://github.com/jpillora/eccjs

根据您的描述,这一行似乎给您带来了错误:

var signature = ecc.sign(user.eccKey, JSON.stringify(message));

所以我猜你没有正确通过 user.eccKey?

您是否拥有来自 oAuth(或任何登录授权)的所有用户数据?还是在用户注册时正确生成了 eccKey?

此外,请尝试包括 ecc.js 的非缩小版本,以便您可以更轻松地跟踪错误的来源。 https://raw.githubusercontent.com/jpillora/eccjs/gh-pages/dist/0.3/ecc.js