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?
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:
message
eventThese steps will be resulted from this code:
Also to justify my statement above let me modify the code a bit using timers:
The execution of the code above will result:
As you can see the diffie hellman generation without prime and generator takes WAY too long instead of provided prime and generator.