使用npm`threads`模块生成Diffie Hellman对象:getPrime未定义

时间:2019-01-10 12:20:47

标签: node.js multithreading diffie-hellman

使用npm threads库,我尝试使用单独的线程而不是主线程以非阻塞方式创建diffie hellman:

const spawn = require('threads').spawn;

const thread = spawn(function(input, done) {
  const cryptot = require('crypto');
  const dh = cryptot.createDiffieHellman(2048);
  done({dh});
});

thread.send({p:null, g:null}).on('message', (response) => {
  console.log(response.dh.getPrime(), response.dh.getGenerator());
  thread.kill();
}).on('error', (err)=>{
  console.error(err);
}).on('exit', function() {
  console.log('Worker has been terminated.');
});

但是出现以下错误:

/home/pcmagas/Kwdikas/master_thesis/custom_xmpp/dummy_src/thread_dh.js:10
  console.log(response.dh.getPrime(), response.dh.getGenerator());
                          ^

TypeError: response.dh.getPrime is not a function
    at Worker.thread.send.on (/home/pcmagas/Kwdikas/master_thesis/custom_xmpp/dummy_src/thread_dh.js:10:27)
    at Worker.emit (/home/pcmagas/Kwdikas/master_thesis/custom_xmpp/node_modules/eventemitter3/index.js:129:35)
    at Worker.handleMessage (/home/pcmagas/Kwdikas/master_thesis/custom_xmpp/node_modules/threads/lib/worker.node/worker.js:148:17)
    at ChildProcess.emit (events.js:182:13)
    at emit (internal/child_process.js:812:12)

您知道为什么接收到的dh对象不包含方法getPrime以及通过假设getGenerator也不包含方法吗?

1 个答案:

答案 0 :(得分:0)

的确,在不使用生成器和启动的情况下,密钥生成在node.js中提供的实现上很慢。但是,它使速度变慢的原因是创建了正确的素数和生成器,其中素数将是 2048 位。

因此您可以执行以下操作:

  1. 在线程内部 生成质数和生成器
  2. 通过完成回调将素数abd生成器传递给message事件
  3. 使用生成的素数和生成器在那里重新创建diffie-hellman对象。

这些步骤将由以下代码完成:

const spawn = require('threads').spawn;

const thread = spawn(function(input, done) {
  const cryptot = require('crypto');
  console.time('dh-thread');
  const dh = cryptot.createDiffieHellman(2048);
  console.timeEnd('dh-thread');
  done({prime: dh.getPrime().toString('hex'), generator: dh.getGenerator().toString('hex')});
});

thread.send({p:null, g:null}).on('message', (response) => {
  const cryptot = require('crypto');
  const dh =  cryptot.createDiffieHellman(response.prime, response.generator);
  // Do whatever you want there
  thread.kill();
}).on('error', (err)=>{
  console.error(err);
}).on('exit', function() {
  console.log('Worker has been terminated.');
});

为了证明我上面的陈述的正确性,让我使用计时器稍微修改一下代码:

const spawn = require('threads').spawn;

const thread = spawn(function(input, done) {
  const cryptot = require('crypto');
  console.time('dh-thread');
  const dh = cryptot.createDiffieHellman(2048);
  console.timeEnd('dh-thread');
  done({prime: dh.getPrime().toString('hex'), generator: dh.getGenerator().toString('hex')});
});

thread.send({p:null, g:null}).on('message', (response) => {
  console.time('dh');
  const cryptot = require('crypto');
  const dh =  cryptot.createDiffieHellman(response.prime, response.generator);
  console.timeEnd('dh');
  thread.kill();
}).on('error', (err)=>{
  console.error(err);
}).on('exit', function() {
  console.log('Worker has been terminated.');
});

执行上面的代码将导致:

dh-thread: 12815.747ms
dh: 6.733ms
Worker has been terminated.

如您所见,没有素数和生成器的diffie hellman生成 WAY 时间过长,而不是提供的素数和生成器。