使用npm`threads`模块生成Diffie Hellman对象:getPrime未定义 [英] Generate Diffie Hellman Object using npm `threads` module: getPrime is undefined

查看:107
本文介绍了使用npm`threads`模块生成Diffie Hellman对象:getPrime未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是出现以下错误:

/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也不包含该方法吗?

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

推荐答案

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

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. 在线程内部 生成质数和生成器
  2. 通过完成回调将主要abd生成器传递给message事件
  3. 在那里使用生成的素数和生成器重新创建diffie-hellman对象.
  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.

如您所见,不带质数和生成器的diffie hellman生成 WAY 时间过长,而不是提供的质数和生成器.

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

这篇关于使用npm`threads`模块生成Diffie Hellman对象:getPrime未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆