[Pkg-javascript-commits] [node-browserify-aes] 02/92: legacy update and final methods

Bastien Roucariès rouca at moszumanska.debian.org
Sun Jun 4 09:35:14 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browserify-aes.

commit 08d287a14bd8369a11c5883567ce87b5ca79359a
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Thu Oct 16 08:41:27 2014 -0400

    legacy update and final methods
---
 decrypter.js  | 77 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 encrypter.js  | 55 ++++++++++++++++++++++++++++++++++++++----
 test/index.js | 53 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 168 insertions(+), 17 deletions(-)

diff --git a/decrypter.js b/decrypter.js
index b64a8da..47df209 100644
--- a/decrypter.js
+++ b/decrypter.js
@@ -6,6 +6,13 @@ var modes = require('./modes');
 var ebtk = require('./EVP_BytesToKey');
 var xor = require('./xor');
 inherits(Splitter, Transform);
+function unpad(last) {
+  var padded = last[15];
+  if (padded === 16) {
+    return;
+  }
+  return last.slice(0, 16 - padded);
+}
 function Splitter() {
   if (!(this instanceof Splitter)) {
     return new Splitter();
@@ -36,6 +43,7 @@ function ECB(key) {
   Transform.call(this);
   this._cipher = new aes.AES(key);
   this._last = void 0;
+  this._pad = true;
 }
 
 ECB.prototype._transform = function (data, _, next) {
@@ -50,13 +58,16 @@ ECB.prototype._transform = function (data, _, next) {
 
 ECB.prototype._flush = function (next) {
   this._cipher.scrub();
-  var last = this._last;
-  var padded = last[15];
-  if (padded === 16) {
+  if (this._pad === false) {
+    if (this._last) {
+      this.push(this._last);
+    }
     return next();
   }
-  var out = last.slice(0, 16 - padded);
-  this.push(out);
+  var depadded = unpad(this._last);
+  if (depadded) {
+    this.push(depadded);
+  }
   next();
 };
 inherits(CBC, Transform);
@@ -78,17 +89,21 @@ CBC.prototype._transform = function (data, _, next) {
   }
   this._last = xor(out, this._prev);
   this._prev = indata;
+  this._pad = true;
   next();
 };
 CBC.prototype._flush = function (next) {
   this._cipher.scrub();
-  var last = this._last;
-  var padded = last[15];
-  if (padded === 16) {
+  if (this._pad === false) {
+    if (this._last) {
+      this.push(this._last);
+    }
     return next();
   }
-  var out = last.slice(0, 16 - padded);
-  this.push(out);
+  var depadded = unpad(this._last);
+  if (depadded) {
+    this.push(depadded);
+  }
   next();
 };
 var modeStreams = {
@@ -116,8 +131,46 @@ module.exports = function (crypto) {
     }
     var splitter = new Splitter();
     var stream = new modeStreams[config.mode](password, iv);
-    splitter.pipe(stream);
-    return duplexer(splitter, stream);
+    splitter.on('data', function (d) {
+      stream.write(d);
+    });
+    splitter.on('finish', function () {
+      stream.end();
+    });
+    var out = duplexer(splitter, stream);
+    out.setAutoPadding = function (padding) {
+      stream._padding = padding;
+    };
+    out._legacy = false;
+    var outData = new Buffer('');
+    out.update = function (data, inputEnd, outputEnc) {
+      if (out._legacy === false) {
+        out._legacy = true;
+        stream.on('data', function (chunk) {
+          outData = Buffer.concat([outData, chunk]);
+        });
+        stream.pause = function (){
+          // else it will stall out
+        };
+      }
+      splitter.write(data, inputEnd);
+      var ourData = outData;
+      outData = new Buffer('');
+      if (outputEnc) {
+        ourData = ourData.toString(outputEnc);
+      }
+      return ourData;
+    };
+    out.final = function (outputEnc) {
+      splitter.end();
+      var ourData = outData;
+      outData = null;
+      if (outputEnc) {
+        ourData = ourData.toString(outputEnc);
+      }
+      return ourData;
+    };
+    return out;
   }
   function createDecipher (suite, password) {
     var config = modes[suite];
diff --git a/encrypter.js b/encrypter.js
index 0366a02..186087a 100644
--- a/encrypter.js
+++ b/encrypter.js
@@ -6,9 +6,14 @@ var modes = require('./modes');
 var ebtk = require('./EVP_BytesToKey');
 var xor = require('./xor');
 inherits(Splitter, Transform);
-function Splitter() {
+function Splitter(padding) {
   if (!(this instanceof Splitter)) {
-    return new Splitter();
+    return new Splitter(padding);
+  }
+  if (padding === false) {
+    this._padding = false;
+  } else {
+    this._padding = true;
   }
   Transform.call(this);
   this.cache = new Buffer('');
@@ -29,6 +34,10 @@ Splitter.prototype._transform = function (data, _, next) {
 };
 
 Splitter.prototype._flush = function (next) {
+  if (!this._padding) {
+    this.push(this.cache);
+    return next();
+  }
   var len = 16 - this.cache.length;
   var padBuff = new Buffer(len);
 
@@ -103,8 +112,46 @@ module.exports = function (crypto) {
     }
     var splitter = new Splitter();
     var stream = new modeStreams[config.mode](password, iv);
-    splitter.pipe(stream);
-    return duplexer(splitter, stream);
+    splitter.on('data', function (d) {
+      stream.write(d);
+    });
+    splitter.on('finish', function () {
+      stream.end();
+    });
+    var out = duplexer(splitter, stream);
+    out.setAutoPadding = function (padding) {
+      splitter._padding = padding;
+    };
+    out._legacy = false;
+    var outData = new Buffer('');
+    out.update = function (data, inputEnd, outputEnc) {
+      if (out._legacy === false) {
+        out._legacy = true;
+        stream.on('data', function (chunk) {
+          outData = Buffer.concat([outData, chunk]);
+        });
+        stream.pause = function (){
+          // else it will stall out
+        };
+      }
+      splitter.write(data, inputEnd);
+      var ourData = outData;
+      outData = new Buffer('');
+      if (outputEnc) {
+        ourData = ourData.toString(outputEnc);
+      }
+      return ourData;
+    };
+    out.final = function (outputEnc) {
+      splitter.end();
+      var ourData = outData;
+      outData = null;
+      if (outputEnc) {
+        ourData = ourData.toString(outputEnc);
+      }
+      return ourData;
+    };
+    return out;
   }
   function createCipher (suite, password) {
     var config = modes[suite];
diff --git a/test/index.js b/test/index.js
index 3b06a37..c8ac5c8 100644
--- a/test/index.js
+++ b/test/index.js
@@ -43,7 +43,19 @@ fixtures.forEach(function (fixture) {
       suite.write(new Buffer(fixture.text));
       suite.end();
     });
-    test(cipher + '-derypt', function (t) {
+    test(cipher + '-legacy', function (t) {
+      t.plan(1);
+      var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
+      var buf = new Buffer('');
+      buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
+      buf = Buffer.concat([buf, suite.final()]);
+      var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password));
+      var buf2 = new Buffer('');
+      buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
+      buf2 = Buffer.concat([buf2, suite2.final()]);
+      t.equals(buf.toString('hex'), buf2.toString('hex'));
+    });
+    test(cipher + '-decrypt', function (t) {
       t.plan(1);
       var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
       var buf = new Buffer('');
@@ -62,6 +74,19 @@ fixtures.forEach(function (fixture) {
       suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'));
       suite.end();
     });
+    test(cipher + '-decrypt-legacy', function (t) {
+      t.plan(2);
+      var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
+      var buf = new Buffer('');
+      buf = Buffer.concat([buf, suite.update(new Buffer(fixture.results.ciphers[cipher], 'hex'))]);
+      buf = Buffer.concat([buf, suite.final()]);
+      var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password));
+      var buf2 = new Buffer('');
+      buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.results.ciphers[cipher], 'hex'))]);
+      buf2 = Buffer.concat([buf2, suite2.final()]);
+      t.equals(buf.toString('utf8'), fixture.text);
+      t.equals(buf.toString('utf8'), buf2.toString('utf8'));
+    });
     //var cipherivs = fixture.results.cipherivs = {};
     types.forEach(function (cipher) {
       if (modes[cipher].mode === 'ECB') {
@@ -83,6 +108,19 @@ fixtures.forEach(function (fixture) {
         suite.write(new Buffer(fixture.text));
         suite.end();
       });
+      test(cipher + '-legacy-iv', function (t) {
+        t.plan(2);
+        var suite = crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
+        var buf = new Buffer('');
+        buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
+        buf = Buffer.concat([buf, suite.final()]);
+        var suite2 = _crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
+        var buf2 = new Buffer('');
+        buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
+        buf2 = Buffer.concat([buf2, suite2.final()]);
+        t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher]);
+        t.equals(buf.toString('hex'), buf2.toString('hex'));
+      });
       test(cipher + '-iv-decrypt', function (t) {
         t.plan(1);
         var suite = crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
@@ -99,6 +137,19 @@ fixtures.forEach(function (fixture) {
         suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
         suite.end();
       });
+      test(cipher + '-decrypt-legacy', function (t) {
+        t.plan(2);
+        var suite = crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
+        var buf = new Buffer('');
+        buf = Buffer.concat([buf, suite.update(new Buffer(fixture.results.cipherivs[cipher], 'hex'))]);
+        buf = Buffer.concat([buf, suite.final()]);
+        var suite2 = _crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
+        var buf2 = new Buffer('');
+        buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.results.cipherivs[cipher], 'hex'))]);
+        buf2 = Buffer.concat([buf2, suite2.final()]);
+        t.equals(buf.toString('utf8'), fixture.text);
+        t.equals(buf.toString('utf8'), buf2.toString('utf8'));
+      });
     });
   });
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browserify-aes.git



More information about the Pkg-javascript-commits mailing list