[Pkg-javascript-commits] [node-browserify-aes] 41/92: autopadding option, do not use stream with update/final
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Jun 4 09:35:18 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 15ad6375c18d2ca77ba41a0249fd3312b1614977
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date: Tue Dec 23 09:53:03 2014 -0500
autopadding option, do not use stream with update/final
---
authCipher.js | 7 +++----
cipherBase.js | 28 ++++++++++++++++------------
decrypter.js | 45 ++++++++++++++++++++++++++++-----------------
encrypter.js | 25 +++++++++++++++++--------
streamCipher.js | 7 +++----
5 files changed, 67 insertions(+), 45 deletions(-)
diff --git a/authCipher.js b/authCipher.js
index 3265a01..dc1b993 100644
--- a/authCipher.js
+++ b/authCipher.js
@@ -28,7 +28,7 @@ function StreamCipher(mode, key, iv, decrypt) {
this._authTag = null;
this._called = false;
}
-StreamCipher.prototype._transform = function (chunk, _, next) {
+StreamCipher.prototype._update = function (chunk) {
if (!this._called && this._alen) {
var rump = 16 - (this._alen % 16);
if (rump <16) {
@@ -45,9 +45,9 @@ StreamCipher.prototype._transform = function (chunk, _, next) {
this._ghash.update(out);
}
this._len += chunk.length;
- next(null, out);
+ return out;
};
-StreamCipher.prototype._flush = function (next) {
+StreamCipher.prototype._final = function () {
if (this._decrypt && !this._authTag) {
throw new Error('Unsupported state or unable to authenticate data');
}
@@ -60,7 +60,6 @@ StreamCipher.prototype._flush = function (next) {
this._authTag = tag;
}
this._cipher.scrub();
- next();
};
StreamCipher.prototype.getAuthTag = function getAuthTag () {
if (!this._decrypt && Buffer.isBuffer(this._authTag)) {
diff --git a/cipherBase.js b/cipherBase.js
index e580048..8dfbb5b 100644
--- a/cipherBase.js
+++ b/cipherBase.js
@@ -6,25 +6,29 @@ inherits(CipherBase, Transform);
function CipherBase() {
Transform.call(this);
}
-CipherBase.prototype.update = function (data, inputEnd, outputEnc) {
- this.write(data, inputEnd);
- var outData = new Buffer('');
- var chunk;
- while ((chunk = this.read())) {
- outData = Buffer.concat([outData, chunk]);
+CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
+ if (typeof data === 'string') {
+ data = new Buffer(data, inputEnc);
}
+ var outData = this._update(data);
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
};
-CipherBase.prototype.final = function (outputEnc) {
- this.end();
- var outData = new Buffer('');
- var chunk;
- while ((chunk = this.read())) {
- outData = Buffer.concat([outData, chunk]);
+CipherBase.prototype._transform = function (data, _, next) {
+ this.push(this._update(data));
+ next();
+};
+CipherBase.prototype._flush = function (next) {
+ var chunk = this._final();
+ if (chunk) {
+ this.push(chunk);
}
+ next();
+};
+CipherBase.prototype.final = function (outputEnc) {
+ var outData = this._final() || new Buffer('');
if (outputEnc) {
outData = outData.toString(outputEnc);
}
diff --git a/decrypter.js b/decrypter.js
index 5f2f4af..6214d06 100644
--- a/decrypter.js
+++ b/decrypter.js
@@ -18,28 +18,30 @@ function Decipher(mode, key, iv) {
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
+ this._autopadding = true;
}
-Decipher.prototype._transform = function (data, _, next) {
+Decipher.prototype._update = function (data) {
this._cache.add(data);
var chunk;
var thing;
- while ((chunk = this._cache.get())) {
+ var out = [];
+ while ((chunk = this._cache.get(this._autopadding))) {
thing = this._mode.decrypt(this, chunk);
- this.push(thing);
+ out.push(thing);
}
- next();
+ return Buffer.concat(out);
};
-Decipher.prototype._flush = function (next) {
+Decipher.prototype._final = function () {
var chunk = this._cache.flush();
- if (!chunk) {
- return next;
+ if (this._autopadding) {
+ return unpad(this._mode.decrypt(this, chunk));
+ } else if (chunk) {
+ throw new Error('data not multiple of block length');
}
-
- this.push(unpad(this._mode.decrypt(this, chunk)));
-
- next();
};
-
+Decipher.prototype.setAutoPadding = function (setTo) {
+ this._autopadding = !!setTo;
+};
function Splitter() {
if (!(this instanceof Splitter)) {
return new Splitter();
@@ -50,11 +52,20 @@ Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data]);
};
-Splitter.prototype.get = function () {
- if (this.cache.length > 16) {
- var out = this.cache.slice(0, 16);
- this.cache = this.cache.slice(16);
- return out;
+Splitter.prototype.get = function (autoPadding) {
+ var out;
+ if (autoPadding) {
+ if (this.cache.length > 16) {
+ out = this.cache.slice(0, 16);
+ this.cache = this.cache.slice(16);
+ return out;
+ }
+ } else {
+ if (this.cache.length >= 16) {
+ out = this.cache.slice(0, 16);
+ this.cache = this.cache.slice(16);
+ return out;
+ }
}
return null;
};
diff --git a/encrypter.js b/encrypter.js
index a63ffbd..bec77f7 100644
--- a/encrypter.js
+++ b/encrypter.js
@@ -16,24 +16,33 @@ function Cipher(mode, key, iv) {
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
+ this._autopadding = true;
}
-Cipher.prototype._transform = function (data, _, next) {
+Cipher.prototype._update = function (data) {
this._cache.add(data);
var chunk;
var thing;
+ var out = [];
while ((chunk = this._cache.get())) {
thing = this._mode.encrypt(this, chunk);
- this.push(thing);
+ out.push(thing);
}
- next();
+ return Buffer.concat(out);
};
-Cipher.prototype._flush = function (next) {
+Cipher.prototype._final = function () {
var chunk = this._cache.flush();
- this.push(this._mode.encrypt(this, chunk));
- this._cipher.scrub();
- next();
+ if (this._autopadding) {
+ chunk = this._mode.encrypt(this, chunk);
+ this._cipher.scrub();
+ return chunk;
+ } else if (chunk.toString('hex') !== '10101010101010101010101010101010') {
+ this._cipher.scrub();
+ throw new Error('data not multiple of block length');
+ }
+};
+Cipher.prototype.setAutoPadding = function (setTo) {
+ this._autopadding = !!setTo;
};
-
function Splitter() {
if (!(this instanceof Splitter)) {
diff --git a/streamCipher.js b/streamCipher.js
index 1c5c9c5..9d1929d 100644
--- a/streamCipher.js
+++ b/streamCipher.js
@@ -17,10 +17,9 @@ function StreamCipher(mode, key, iv, decrypt) {
iv.copy(this._prev);
this._mode = mode;
}
-StreamCipher.prototype._transform = function (chunk, _, next) {
- next(null, this._mode.encrypt(this, chunk, this._decrypt));
+StreamCipher.prototype._update = function (chunk) {
+ return this._mode.encrypt(this, chunk, this._decrypt);
};
-StreamCipher.prototype._flush = function (next) {
+StreamCipher.prototype._final = function () {
this._cipher.scrub();
- next();
};
\ No newline at end of file
--
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