diff --git a/lib/crypto/aes.js b/lib/crypto/aes.js index 26b71215..f7ac9523 100644 --- a/lib/crypto/aes.js +++ b/lib/crypto/aes.js @@ -490,7 +490,7 @@ AESCipher.prototype.update = function update(data) { var i, len, trailing, block; if (this.waiting) { - data = Buffer.concat([this.waiting, data]); + data = concat(this.waiting, data); this.waiting = null; } @@ -529,7 +529,7 @@ AESCipher.prototype.final = function final() { left = 16 - block.length; pad = new Buffer(left); pad.fill(left); - block = Buffer.concat([block, pad]); + block = concat(block, pad); } // Encrypt the last block, @@ -577,7 +577,7 @@ AESDecipher.prototype.update = function update(data) { var i, chunk, block, len, trailing; if (this.waiting) { - data = Buffer.concat([this.waiting, data]); + data = concat(this.waiting, data); this.waiting = null; } @@ -656,10 +656,7 @@ AESDecipher.prototype.final = function final() { AES.encrypt = function encrypt(data, key, iv, bits, mode) { var cipher = new AESCipher(key, iv, bits, mode); - return Buffer.concat([ - cipher.update(data), - cipher.final() - ]); + return concat(cipher.update(data), cipher.final()); }; /** @@ -674,10 +671,7 @@ AES.encrypt = function encrypt(data, key, iv, bits, mode) { AES.decrypt = function decrypt(data, key, iv, bits, mode) { var decipher = new AESDecipher(key, iv, bits, mode); - return Buffer.concat([ - decipher.update(data), - decipher.final() - ]); + return concat(decipher.update(data), decipher.final()); }; /** @@ -771,6 +765,13 @@ function writeU32(data, value, i) { data[i + 3] = value & 0xff; } +function concat(a, b) { + var data = new Buffer(a.length + b.length); + a.copy(data, 0); + b.copy(data, a.length); + return data; +} + /* * Tables */ diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index fece8222..09b6388f 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -299,10 +299,7 @@ crypto.encipher = function encipher(data, key, iv) { cipher = nodeCrypto.createCipheriv('aes-256-cbc', key, iv); - return Buffer.concat([ - cipher.update(data), - cipher.final() - ]); + return utils.concat(cipher.update(data), cipher.final()); }; /** @@ -350,10 +347,7 @@ crypto.decipher = function decipher(data, key, iv) { decipher = nodeCrypto.createDecipheriv('aes-256-cbc', key, iv); - return Buffer.concat([ - decipher.update(data), - decipher.final() - ]); + return utils.concat(decipher.update(data), decipher.final()); }; /** diff --git a/lib/script/script.js b/lib/script/script.js index 1a5cf3f2..45358717 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -979,7 +979,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { throw new ScriptError('INVALID_STACK_OPERATION', op, ip); v2 = stack.pop(); v1 = stack.pop(); - stack.push(Buffer.concat([v1, v2])); + stack.push(utils.concat(v1, v2)); if (stack.top(-1).length > constants.script.MAX_PUSH) throw new ScriptError('PUSH_SIZE', op, ip); break; @@ -1035,12 +1035,12 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { if (v1.length < v2.length) { v3 = new Buffer(v2.length - v1.length); v3.fill(0); - v1 = Buffer.concat([v1, v3]); + v1 = utils.concat(v1, v3); } if (v2.length < v1.length) { v3 = new Buffer(v1.length - v2.length); v3.fill(0); - v2 = Buffer.concat([v2, v3]); + v2 = utils.concat(v2, v3); } if (op === opcodes.OP_AND) { for (i = 0; i < v1.length; i++) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 148b2480..07b0ba9c 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -103,6 +103,20 @@ utils.copy = function copy(data) { return clone; }; +/** + * Concatenate two buffers. + * @param {Buffer} a + * @param {Buffer} b + * @returns {Buffer} + */ + +utils.concat = function concat(a, b) { + var data = new Buffer(a.length + b.length); + a.copy(data, 0); + b.copy(data, a.length); + return data; +}; + /** * Encode a base58 string. * @see https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp