utils: concat.

This commit is contained in:
Christopher Jeffrey 2016-10-02 03:22:46 -07:00
parent 1e362c8166
commit e850227793
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 31 additions and 22 deletions

View File

@ -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
*/

View File

@ -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());
};
/**

View File

@ -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++)

View File

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