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:
- Generate the prime and the generator inside the thread
- Pass the prime abd generator via done callback to the
message
event
- 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.