Browse Source

crypto: initialize transform lazily

v0.10.1-release
Fedor Indutny 12 years ago
committed by isaacs
parent
commit
855caa82aa
  1. 42
      lib/crypto.js

42
lib/crypto.js

@ -150,15 +150,33 @@ exports.createCredentials = function(options, context) {
}; };
function LazyTransform(options) {
this._options = options;
}
util.inherits(LazyTransform, stream.Transform);
['read', 'write', 'end'].forEach(function(action, i, actions) {
LazyTransform.prototype[action] = function() {
stream.Transform.call(this, this._options);
actions.forEach(function(action) {
this[action] = stream.Transform.prototype[action];
}, this);
return this[action].apply(this, arguments);
};
});
exports.createHash = exports.Hash = Hash; exports.createHash = exports.Hash = Hash;
function Hash(algorithm, options) { function Hash(algorithm, options) {
if (!(this instanceof Hash)) if (!(this instanceof Hash))
return new Hash(algorithm); return new Hash(algorithm);
this._binding = new binding.Hash(algorithm); this._binding = new binding.Hash(algorithm);
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Hash, stream.Transform); util.inherits(Hash, LazyTransform);
Hash.prototype._transform = function(chunk, encoding, callback) { Hash.prototype._transform = function(chunk, encoding, callback) {
this._binding.update(chunk, encoding); this._binding.update(chunk, encoding);
@ -194,10 +212,10 @@ function Hmac(hmac, key, options) {
return new Hmac(hmac, key); return new Hmac(hmac, key);
this._binding = new binding.Hmac(); this._binding = new binding.Hmac();
this._binding.init(hmac, toBuf(key)); this._binding.init(hmac, toBuf(key));
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Hmac, stream.Transform); util.inherits(Hmac, LazyTransform);
Hmac.prototype.update = Hash.prototype.update; Hmac.prototype.update = Hash.prototype.update;
Hmac.prototype.digest = Hash.prototype.digest; Hmac.prototype.digest = Hash.prototype.digest;
@ -221,10 +239,10 @@ function Cipher(cipher, password, options) {
this._binding.init(cipher, toBuf(password)); this._binding.init(cipher, toBuf(password));
this._decoder = null; this._decoder = null;
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Cipher, stream.Transform); util.inherits(Cipher, LazyTransform);
Cipher.prototype._transform = function(chunk, encoding, callback) { Cipher.prototype._transform = function(chunk, encoding, callback) {
this.push(this._binding.update(chunk, encoding)); this.push(this._binding.update(chunk, encoding));
@ -280,10 +298,10 @@ function Cipheriv(cipher, key, iv, options) {
this._binding.initiv(cipher, toBuf(key), toBuf(iv)); this._binding.initiv(cipher, toBuf(key), toBuf(iv));
this._decoder = null; this._decoder = null;
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Cipheriv, stream.Transform); util.inherits(Cipheriv, LazyTransform);
Cipheriv.prototype._transform = Cipher.prototype._transform; Cipheriv.prototype._transform = Cipher.prototype._transform;
Cipheriv.prototype._flush = Cipher.prototype._flush; Cipheriv.prototype._flush = Cipher.prototype._flush;
@ -302,10 +320,10 @@ function Decipher(cipher, password, options) {
this._binding.init(cipher, toBuf(password)); this._binding.init(cipher, toBuf(password));
this._decoder = null; this._decoder = null;
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Decipher, stream.Transform); util.inherits(Decipher, LazyTransform);
Decipher.prototype._transform = Cipher.prototype._transform; Decipher.prototype._transform = Cipher.prototype._transform;
Decipher.prototype._flush = Cipher.prototype._flush; Decipher.prototype._flush = Cipher.prototype._flush;
@ -325,10 +343,10 @@ function Decipheriv(cipher, key, iv, options) {
this._binding.initiv(cipher, toBuf(key), toBuf(iv)); this._binding.initiv(cipher, toBuf(key), toBuf(iv));
this._decoder = null; this._decoder = null;
stream.Transform.call(this, options); LazyTransform.call(this, options);
} }
util.inherits(Decipheriv, stream.Transform); util.inherits(Decipheriv, LazyTransform);
Decipheriv.prototype._transform = Cipher.prototype._transform; Decipheriv.prototype._transform = Cipher.prototype._transform;
Decipheriv.prototype._flush = Cipher.prototype._flush; Decipheriv.prototype._flush = Cipher.prototype._flush;

Loading…
Cancel
Save