[Pkg-javascript-commits] [node-browserify-aes] 76/92: fixes #26- issues with encoding beyond base64

Bastien Roucariès rouca at moszumanska.debian.org
Sun Jun 4 09:35:21 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 2c36168113e8ca93d4d09df4472f4a53debcd0e5
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Wed Sep 23 09:03:34 2015 -0400

    fixes #26- issues with encoding beyond base64
---
 cipherBase.js | 36 ++++++++++++------------------------
 test/index.js | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/cipherBase.js b/cipherBase.js
index 26928d0..ad828d9 100644
--- a/cipherBase.js
+++ b/cipherBase.js
@@ -1,11 +1,12 @@
 var Transform = require('stream').Transform
 var inherits = require('inherits')
-
+var StringDecoder = require('string_decoder').StringDecoder
 module.exports = CipherBase
 inherits(CipherBase, Transform)
 function CipherBase () {
   Transform.call(this)
-  this._base64Cache = new Buffer('')
+  this._decoder = null
+  this._encoding = null
 }
 CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
   if (typeof data === 'string') {
@@ -38,29 +39,16 @@ CipherBase.prototype.final = function (outputEnc) {
 }
 
 CipherBase.prototype._toString = function (value, enc, final) {
-  if (enc !== 'base64') {
-    return value.toString(enc)
-  }
-  this._base64Cache = Buffer.concat([this._base64Cache, value])
-  var out
-  if (final) {
-    out = this._base64Cache
-    this._base64Cache = null
-    return out.toString('base64')
+  if (!this._decoder) {
+    this._decoder = new StringDecoder(enc)
+    this._encoding = enc
   }
-  var len = this._base64Cache.length
-  var overhang = len % 3
-  if (!overhang) {
-    out = this._base64Cache
-    this._base64Cache = new Buffer('')
-    return out.toString('base64')
+  if (this._encoding !== enc) {
+    throw new Error('can\'t switch encodings')
   }
-  var newLen = len - overhang
-  if (!newLen) {
-    return ''
+  var out = this._decoder.write(value)
+  if (final) {
+    out += this._decoder.end()
   }
-
-  out = this._base64Cache.slice(0, newLen)
-  this._base64Cache = this._base64Cache.slice(-overhang)
-  return out.toString('base64')
+  return out
 }
diff --git a/test/index.js b/test/index.js
index b57e085..4cbcaf6 100644
--- a/test/index.js
+++ b/test/index.js
@@ -530,3 +530,40 @@ test('correctly handle incremental base64 output', function (t) {
   var decrypted = decrypt(encrypted, key)
   t.equals(data, decrypted, 'round trips')
 })
+
+test('handle long uft8 plaintexts', function (t) {
+  t.plan(1)
+  var salt = new Buffer(32)
+  salt.fill(0)
+  function encrypt (txt, passwd) {
+    var cipher = crypto.createCipher('aes-256-cbc', salt)
+    var result = cipher.update(txt, 'utf8', 'base64')
+    result += cipher.final('base64')
+    return result
+  }
+  function decrypt (enc, passwd) {
+    var decipher = crypto.createDecipher('aes-256-cbc', salt)
+
+    return decipher.update(enc, 'base64', 'utf8') + decipher.final('utf8')
+  }
+  var input = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん'
+  var enc = encrypt(input, 'a')
+
+  var dec = decrypt(enc, 'a')
+  t.equals(dec, input)
+})
+
+test('mix and match encoding', function (t) {
+  t.plan(2)
+  var cipher = crypto.createCipher('aes-256-cbc', 'a')
+  cipher.update('foo', 'utf8', 'utf8')
+  t.throws(function () {
+    cipher.update('foo', 'utf8', 'base64')
+  })
+  cipher = crypto.createCipher('aes-256-cbc', 'a')
+  cipher.update('foo', 'utf8', 'base64')
+  t.doesNotThrow(function () {
+    cipher.update('foo', 'utf8')
+    cipher.final('base64')
+  })
+})

-- 
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