[Pkg-javascript-commits] [node-browserify-aes] 71/92: correctly handle incremental base64 data

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 8d40455a284a6bf99673cbf0f964f2423809b8e8
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Mon Jul 20 09:21:40 2015 -0400

    correctly handle incremental base64 data
---
 cipherBase.js | 33 +++++++++++++++++++++++++++++++--
 test/index.js | 30 ++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/cipherBase.js b/cipherBase.js
index 45450f8..26928d0 100644
--- a/cipherBase.js
+++ b/cipherBase.js
@@ -5,6 +5,7 @@ module.exports = CipherBase
 inherits(CipherBase, Transform)
 function CipherBase () {
   Transform.call(this)
+  this._base64Cache = new Buffer('')
 }
 CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
   if (typeof data === 'string') {
@@ -12,7 +13,7 @@ CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
   }
   var outData = this._update(data)
   if (outputEnc) {
-    outData = outData.toString(outputEnc)
+    outData = this._toString(outData, outputEnc)
   }
   return outData
 }
@@ -31,7 +32,35 @@ CipherBase.prototype._flush = function (next) {
 CipherBase.prototype.final = function (outputEnc) {
   var outData = this._final() || new Buffer('')
   if (outputEnc) {
-    outData = outData.toString(outputEnc)
+    outData = this._toString(outData, outputEnc, true)
   }
   return outData
 }
+
+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')
+  }
+  var len = this._base64Cache.length
+  var overhang = len % 3
+  if (!overhang) {
+    out = this._base64Cache
+    this._base64Cache = new Buffer('')
+    return out.toString('base64')
+  }
+  var newLen = len - overhang
+  if (!newLen) {
+    return ''
+  }
+
+  out = this._base64Cache.slice(0, newLen)
+  this._base64Cache = this._base64Cache.slice(-overhang)
+  return out.toString('base64')
+}
diff --git a/test/index.js b/test/index.js
index 5e4166c..b57e085 100644
--- a/test/index.js
+++ b/test/index.js
@@ -500,3 +500,33 @@ test('getCiphers works', function (t) {
   t.plan(1)
   t.ok(crypto.getCiphers().length, 'get some ciphers')
 })
+
+test('correctly handle incremental base64 output', function (t) {
+  t.plan(2)
+  var encoding = 'base64'
+  function encrypt (data, key, algorithm) {
+    algorithm = algorithm || 'aes256'
+    var cipher = crypto.createCipher(algorithm, key)
+    var part1 = cipher.update(data, 'utf8', encoding)
+    var part2 = cipher.final(encoding)
+    return part1 + part2
+  }
+  function encryptNode (data, key, algorithm) {
+    algorithm = algorithm || 'aes256'
+    var cipher = _crypto.createCipher(algorithm, key)
+    var part1 = cipher.update(data, 'utf8', encoding)
+    var part2 = cipher.final(encoding)
+    return part1 + part2
+  }
+  function decrypt (data, key, algorithm) {
+    algorithm = algorithm || 'aes256'
+    var decipher = crypto.createDecipher(algorithm, key)
+    return decipher.update(data, encoding, 'utf8') + decipher.final('utf8')
+  }
+  var key = 'this is a very secure key'
+  var data = 'The quick brown fox jumps over the lazy dog.'
+  var encrypted = encrypt(data, key)
+  t.equals(encrypted, encryptNode(data, key), 'encrypt correctly')
+  var decrypted = decrypt(encrypted, key)
+  t.equals(data, decrypted, 'round trips')
+})

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