From e336134658fe67265e4e0497e7972c088c5bb43a Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 29 Oct 2012 16:36:20 -0700 Subject: [PATCH] crypto: Streaming interface for cipher/decipher/iv --- lib/crypto.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index 3807bf4..e6b0396 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -213,15 +213,28 @@ function getDecoder(decoder, encoding) { exports.createCipher = exports.Cipher = Cipher; -function Cipher(cipher, password) { +function Cipher(cipher, password, options) { if (!(this instanceof Cipher)) return new Cipher(cipher, password); this._binding = new binding.Cipher; this._binding.init(cipher, toBuf(password)); this._decoder = null; + + stream.Transform.call(this, options); } +util.inherits(Cipher, stream.Transform); + +Cipher.prototype._transform = function(chunk, output, callback) { + output(this._binding.update(chunk)); + callback(); +}; + +Cipher.prototype._flush = function(output, callback) { + output(this._binding.final()); + callback(); +}; Cipher.prototype.update = function(data, inputEncoding, outputEncoding) { inputEncoding = inputEncoding || exports.DEFAULT_ENCODING; @@ -260,15 +273,20 @@ Cipher.prototype.setAutoPadding = function(ap) { exports.createCipheriv = exports.Cipheriv = Cipheriv; -function Cipheriv(cipher, key, iv) { +function Cipheriv(cipher, key, iv, options) { if (!(this instanceof Cipheriv)) return new Cipheriv(cipher, key, iv); this._binding = new binding.Cipher(); this._binding.initiv(cipher, toBuf(key), toBuf(iv)); this._decoder = null; + + stream.Transform.call(this, options); } +util.inherits(Cipheriv, stream.Transform); +Cipheriv.prototype._transform = Cipher.prototype._transform; +Cipheriv.prototype._flush = Cipher.prototype._flush; Cipheriv.prototype.update = Cipher.prototype.update; Cipheriv.prototype.final = Cipher.prototype.final; Cipheriv.prototype.setAutoPadding = Cipher.prototype.setAutoPadding; @@ -276,16 +294,21 @@ Cipheriv.prototype.setAutoPadding = Cipher.prototype.setAutoPadding; exports.createDecipher = exports.Decipher = Decipher; -function Decipher(cipher, password) { +function Decipher(cipher, password, options) { if (!(this instanceof Decipher)) return new Decipher(cipher, password); this._binding = new binding.Decipher; this._binding.init(cipher, toBuf(password)); this._decoder = null; + + stream.Transform.call(this, options); } +util.inherits(Decipher, stream.Transform); +Decipher.prototype._transform = Cipher.prototype._transform; +Decipher.prototype._flush = Cipher.prototype._flush; Decipher.prototype.update = Cipher.prototype.update; Decipher.prototype.final = Cipher.prototype.final; Decipher.prototype.finaltol = Cipher.prototype.final; @@ -294,16 +317,21 @@ Decipher.prototype.setAutoPadding = Cipher.prototype.setAutoPadding; exports.createDecipheriv = exports.Decipheriv = Decipheriv; -function Decipheriv(cipher, key, iv) { +function Decipheriv(cipher, key, iv, options) { if (!(this instanceof Decipheriv)) return new Decipheriv(cipher, key, iv); this._binding = new binding.Decipher; this._binding.initiv(cipher, toBuf(key), toBuf(iv)); this._decoder = null; + + stream.Transform.call(this, options); } +util.inherits(Decipheriv, stream.Transform); +Decipheriv.prototype._transform = Cipher.prototype._transform; +Decipheriv.prototype._flush = Cipher.prototype._flush; Decipheriv.prototype.update = Cipher.prototype.update; Decipheriv.prototype.final = Cipher.prototype.final; Decipheriv.prototype.finaltol = Cipher.prototype.final; -- 2.7.4