Generate Diffie Hellman Object using npm `threads` module: getPrime is undefined

会有一股神秘感。 提交于 2019-12-13 03:23:09

问题


Using from npm threads library I try to create a diffie hellman in a non-blocking manner using a seperate thread instead of the main one:

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.');
});

But I get the following error:

/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)

Do you know why the received dh object does not contain the method getPrime and via an assumption the getGenerator as well?


回答1:


Well its it true that without proviting generator and prime the key generation is slow on the implementation provided in node.js. But what it makes it slow is the creation of the correct prime and generator where the prime will be 2048 bits.

So you can do the following:

  1. Generate the prime and the generator inside the thread
  2. Pass the prime abd generator via done callback to the message event
  3. Re-create a diffie-hellman object there with the generated prime and generator.

These steps will be resulted from this code:

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.');
});

Also to justify my statement above let me modify the code a bit using timers:

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.');
});

The execution of the code above will result:

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

As you can see the diffie hellman generation without prime and generator takes WAY too long instead of provided prime and generator.



来源:https://stackoverflow.com/questions/54128604/generate-diffie-hellman-object-using-npm-threads-module-getprime-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!