Created
July 17, 2017 12:09
-
-
Save jankeromnes/06a1d6c2719a5ed6d37d4fdeb068be84 to your computer and use it in GitHub Desktop.
Benchmarking RSA certificate generation speed (PEM vs RSA Compat vs OpenSSL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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