Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jankeromnes/06a1d6c2719a5ed6d37d4fdeb068be84 to your computer and use it in GitHub Desktop.
Save jankeromnes/06a1d6c2719a5ed6d37d4fdeb068be84 to your computer and use it in GitHub Desktop.
Benchmarking RSA certificate generation speed (PEM vs RSA Compat vs OpenSSL)
let exec = require('child_process').exec;
let forge = require('node-forge');
let rsa = require('rsa-compat').RSA;
let pem = require('pem');
function generateWithOpenSSL (callback) {
let start = Date.now();
exec('openssl genrsa 4096', (error, stdout, stderr) => {
let stop = Date.now();
callback(stop - start);
});
}
function generateWithForge (callback) {
let start = Date.now();
let parameters = {
bits: 4096,
workers: -1 // Auto-detect web workers to accelerate generation.
};
forge.pki.rsa.generateKeyPair(parameters, (error, keypair) => {
let keys = {
pub: forge.ssh.publicKeyToOpenSSH(keypair.publicKey),
priv: forge.pki.privateKeyToPem(keypair.privateKey)
};
let stop = Date.now();
// console.log('forge', error, keys.pub);
callback(stop - start);
});
}
function generateWithRsaCompat (callback) {
let start = Date.now();
let bitlen = 4096;
let exp = 65537;
let options = { public: false, pem: true, internal: false };
rsa.generateKeypair(bitlen, exp, options, function (error, keypair) {
let stop = Date.now();
// console.log('rsa-compat', error, keypair.privateKeyPem);
callback(stop - start);
});
}
function generateWithPem (callback) {
let start = Date.now();
pem.createPrivateKey(4096, (error, key) => {
let stop = Date.now();
// console.log('pem', error, key);
callback(stop - start);
});
}
function test (func, iterations, sum, callback) {
if (iterations < 1) {
callback(sum);
return;
}
func((time) => {
test(func, iterations - 1, sum + time, callback);
});
}
test(generateWithPem, 100, 0, (sum) => {
console.log('PEM:', sum / 100);
test(generateWithRsaCompat, 100, 0, (sum) => {
console.log('RSACOMPAT:', sum / 100);
test(generateWithOpenSSL, 100, 0, (sum) => {
console.log('OPENSSL:', sum / 100);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment