Skip to content

Instantly share code, notes, and snippets.

@naomik naomik/0_8_20.js
Last active Dec 13, 2015

Embed
What would you like to do?
node-0.9.9 cipher streams are slow! 4.40x slower aes192; 3.7x slower for aes256
// aes192 talk x 42,537 ops/sec ±2.00% (92 runs sampled)
// aes256 talk x 29,539 ops/sec ±1.96% (95 runs sampled)
var crypto = require('crypto'),
benchmark = require("benchmark");
var User = function User(name, mod, cipher) {
this.name = name;
// generate keys
this.gen = crypto.getDiffieHellman(mod);
this.gen.generateKeys();
this.publicKey = this.gen.getPublicKey('hex');
this.privateKey = this.gen.getPrivateKey('hex');
this.cipher = cipher || 'aes256';
};
User.prototype.computeSecret = function computeSecret(otherKey) {
return this.secret = this.gen.computeSecret(otherKey, 'hex', 'hex');
};
User.prototype.encode = function encode(message) {
var cipher = crypto.createCipher(this.cipher, this.secret);
// usable api allows me to choose output format
cipher.update(message, "utf8", "hex");
return cipher.final("hex");
};
User.prototype.decode = function decode(message) {
var decipher = crypto.createDecipher(this.cipher, this.secret);
// more handy api!
decipher.update(message, "hex", "utf8");
return decipher.final("utf8");
};
// -------------------------------------------------------------
// SETUP CLIENTS
// setup aes192 clients
var bob_aes192 = new User('bob', 'modp14', 'aes192');
var alice_aes192 = new User('alice', 'modp14', 'aes192');
alice_aes192.computeSecret(bob_aes192.publicKey);
bob_aes192.computeSecret(alice_aes192.publicKey);
// setup aes256 clients
var bob_aes256 = new User('bob', 'modp16', 'aes256');
var alice_aes256 = new User('alice', 'modp16', 'aes256');
alice_aes256.computeSecret(bob_aes256.publicKey);
bob_aes256.computeSecret(alice_aes256.publicKey);
// -------------------------------------------------------------
// BENCHMARK TIME!
var talk = new benchmark.Suite();
talk.add('aes192 talk', function() {
var str = bob_aes192.encode("hello world");
alice_aes192.decode(str);
});
talk.add('aes256 talk', function() {
var str = bob_aes256.encode("hello world");
alice_aes256.decode(str);
});
talk.on('cycle', function(event) {
console.log(String(event.target));
});
talk.run();
// aes192 talk x 9,670 ops/sec ±4.84% (76 runs sampled)
// aes256 talk x 7,841 ops/sec ±8.20% (72 runs sampled)
var crypto = require('crypto'),
benchmark = require("benchmark");
var User = function User(name, mod, cipher) {
this.name = name;
// generate keys
this.gen = crypto.getDiffieHellman(mod);
this.gen.generateKeys();
this.publicKey = this.gen.getPublicKey('hex');
this.privateKey = this.gen.getPrivateKey('hex');
this.cipher = cipher || 'aes256';
};
User.prototype.computeSecret = function computeSecret(otherKey) {
return this.secret = this.gen.computeSecret(otherKey, 'hex', 'hex');
};
User.prototype.encode = function encode(message) {
var cipher = crypto.createCipher(this.cipher, this.secret);
// look at this highly unusable api compared to the previous;
cipher.end(message);
return cipher.read();
};
User.prototype.decode = function decode(message) {
var decipher = crypto.createDecipher(this.cipher, this.secret);
// more gross api
decipher.end(message);
return decipher.read();
};
// -------------------------------------------------------------
// SETUP CLIENTS
// setup aes192 clients
var bob_aes192 = new User('bob', 'modp14', 'aes192');
var alice_aes192 = new User('alice', 'modp14', 'aes192');
alice_aes192.computeSecret(bob_aes192.publicKey);
bob_aes192.computeSecret(alice_aes192.publicKey);
// setup aes256 clients
var bob_aes256 = new User('bob', 'modp16', 'aes256');
var alice_aes256 = new User('alice', 'modp16', 'aes256');
alice_aes256.computeSecret(bob_aes256.publicKey);
bob_aes256.computeSecret(alice_aes256.publicKey);
// -------------------------------------------------------------
// BENCHMARK TIME!
var talk = new benchmark.Suite();
talk.add('aes192 talk', function() {
var str = bob_aes192.encode("hello world");
alice_aes192.decode(str);
});
talk.add('aes256 talk', function() {
var str = bob_aes256.encode("hello world");
alice_aes256.decode(str);
});
talk.on('cycle', function(event) {
console.log(String(event.target));
});
talk.run();
@isaacs

This comment has been minimized.

Copy link

isaacs commented Feb 19, 2013

You know, you can still use the same API on v0.9. Also, you can choose the output format with stream.setEncoding('hex') and choose the input format with stream.write(someString, 'utf8'), just like with every other string.

I'm porting this benchmark to the benchmark-refactor-2 branch now, so that we can analyze it in more detail, and in an automated way. Thanks for helping :)

@isaacs

This comment has been minimized.

Copy link

isaacs commented Feb 19, 2013

By the way, this is a bug: https://gist.github.com/naomik/4967659#file-0_8_20-js-L25

update() can return some portion of the data, and you're dropping it on the floor. This only doesn't fail because you are writing such a short message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.