Socket
Socket
Sign inDemoInstall

browserify-aes

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browserify-aes - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

modes/cbc.js

1

aes.js

@@ -1,2 +0,1 @@

var crypto = require('crypto');
var uint_max = Math.pow(2, 32);

@@ -3,0 +2,0 @@ function fixup_uint32(x) {

var aes = require('./aes');
var Transform = require('stream').Transform;
var inherits = require('inherits');
var duplexer = require('duplexer2');
var modes = require('./modes');
var ebtk = require('./EVP_BytesToKey');
var xor = require('./xor');
inherits(Splitter, Transform);
function unpad(last) {
var padded = last[15];
if (padded === 16) {
return;
inherits(Decipher, Transform);
function Decipher(padding, mode, key, iv) {
if (!(this instanceof Decipher)) {
return new Decipher(padding, mode, key, iv);
}
return last.slice(0, 16 - padded);
}
function Splitter(padding) {
if (!(this instanceof Splitter)) {
return new Splitter(padding);
}
if (padding === false) {
Transform.call(this);
this._cache = new Splitter();
if (padding === false) {
this._padding = false;

@@ -25,179 +19,93 @@ } else {

}
Transform.call(this);
this.cache = new Buffer('');
this._last = void 0;
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
}
Splitter.prototype._transform = function (data, _, next) {
this.cache = Buffer.concat([this.cache, data]);
var i = 0;
var len = this.cache.length;
while (i + 15 < len) {
this.push(this.cache.slice(i, i + 16));
i += 16;
Decipher.prototype._transform = function (data, _, next) {
this._cache.add(data);
var chunk;
var thing;
while ((chunk = this._cache.get())) {
thing = this._mode.decrypt(this, chunk);
this.push(thing);
}
if (i) {
this.cache = this.cache.slice(i);
}
next();
};
Splitter.prototype._flush = function (next) {
if (this._padding === false) {
this.push(this.cache);
Decipher.prototype._flush = function (next) {
var chunk = this._cache.flush();
if (!chunk) {
return next;
}
if (this._padding) {
this.push(unpad(this._mode.decrypt(this, chunk)));
} else {
this.push(this._mode.decrypt(this, chunk));
}
next();
};
inherits(ECB, Transform);
function ECB(key) {
if (!(this instanceof ECB)) {
return new ECB(key);
Decipher.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]);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._last = void 0;
this._pad = true;
}
ECB.prototype._transform = function (data, _, next) {
var last = this._last;
if (last) {
this.push(last);
if (outputEnc) {
outData = outData.toString(outputEnc);
}
this._last = this._cipher.decryptBlock(data);
next(null);
return outData;
};
ECB.prototype._flush = function (next) {
this._cipher.scrub();
if (this._pad === false) {
if (this._last) {
this.push(this._last);
}
return next();
Decipher.prototype.final = function (outputEnc) {
this.end();
var outData = new Buffer('');
var chunk;
while ((chunk = this.read())) {
outData = Buffer.concat([outData, chunk]);
}
var depadded = unpad(this._last);
if (depadded) {
this.push(depadded);
if (outputEnc) {
outData = outData.toString(outputEnc);
}
next();
return outData;
};
inherits(CBC, Transform);
function CBC(key, iv) {
if (!(this instanceof CBC)) {
return new CBC(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
this._last = void 0;
}
CBC.prototype._transform = function (data, _, next) {
var indata = data;
var out = this._cipher.decryptBlock(data);
if (this._last) {
this.push(this._last);
function Splitter() {
if (!(this instanceof Splitter)) {
return new Splitter();
}
this._last = xor(out, this._prev);
this._prev = indata;
this._pad = true;
next();
};
CBC.prototype._flush = function (next) {
this._cipher.scrub();
if (this._pad === false) {
if (this._last) {
this.push(this._last);
}
return next();
}
var depadded = unpad(this._last);
if (depadded) {
this.push(depadded);
}
next();
};
inherits(CFB, Transform);
function CFB(key, iv) {
if (!(this instanceof CFB)) {
return new CFB(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
this.cache = new Buffer('');
}
CFB.prototype._transform = function (data, _, next) {
// yes encrypt
var pad = this._cipher.encryptBlock(this._prev);
this._prev = data;
next(null, xor(pad, data));
Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data]);
};
CFB.prototype._flush = function (next) {
this._cipher.scrub();
next();
};
//the same as encryption
inherits(OFB, Transform);
function OFB(key, iv) {
if (!(this instanceof OFB)) {
return new OFB(key, iv);
Splitter.prototype.get = function () {
if (this.cache.length > 16) {
var out = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
return out;
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
}
OFB.prototype._transform = function (data, _, next) {
this._prev = this._cipher.encryptBlock(this._prev);
next(null, xor(data, this._prev));
return null;
};
OFB.prototype._flush = function (next) {
this._cipher.scrub();
next();
Splitter.prototype.flush = function () {
if (this.cache.length) {
return this.cache;
}
};
inherits(CTR, Transform);
function CTR(key, iv) {
if (!(this instanceof CTR)) {
return new CTR(key, iv);
function unpad(last) {
var padded = last[15];
if (padded === 16) {
return;
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._iv = new Buffer(iv.length);
iv.copy(this._iv);
return last.slice(0, 16 - padded);
}
CTR.prototype._transform = function (data, _, next) {
this.push(xor(data, this._cipher.encryptBlock(this._iv)));
this._incr32();
next();
var modelist = {
ECB: require('./modes/ecb'),
CBC: require('./modes/cbc'),
CFB: require('./modes/cfb'),
OFB: require('./modes/ofb'),
CTR: require('./modes/ctr')
};
CTR.prototype._flush = function (next) {
this._cipher.scrub();
this._iv.fill(0);
next();
};
CTR.prototype._incr32 = function () {
var len = this._iv.length;
var item;
while (len--) {
item = this._iv.readUInt8(len);
if (item === 255) {
this._iv.writeUInt8(0, len);
} else {
item++;
this._iv.writeUInt8(item, len);
break;
}
}
};
var modeStreams = {
ECB: ECB,
CBC: CBC,
CFB: CFB,
OFB: OFB,
CTR: CTR
};

@@ -222,45 +130,5 @@ module.exports = function (crypto) {

}
var splitter = new Splitter(config.padding);
var stream = new modeStreams[config.mode](password, iv);
splitter.on('data', function (d) {
stream.write(d);
});
splitter.on('finish', function () {
stream.end();
});
var out = duplexer(splitter, stream);
out.setAutoPadding = function (padding) {
stream._padding = padding;
};
out._legacy = false;
var outData = new Buffer('');
out.update = function (data, inputEnd, outputEnc) {
if (out._legacy === false) {
out._legacy = true;
stream.on('data', function (chunk) {
outData = Buffer.concat([outData, chunk]);
});
stream.pause = function (){
// else it will stall out
};
}
splitter.write(data, inputEnd);
var ourData = outData;
outData = new Buffer('');
if (outputEnc) {
ourData = ourData.toString(outputEnc);
}
return ourData;
};
out.final = function (outputEnc) {
splitter.end();
var ourData = outData;
outData = null;
if (outputEnc) {
ourData = ourData.toString(outputEnc);
}
return ourData;
};
return out;
return new Decipher(config.padding, modelist[config.mode], password, iv);
}
function createDecipher (suite, password) {

@@ -267,0 +135,0 @@ var config = modes[suite];

var aes = require('./aes');
var Transform = require('stream').Transform;
var inherits = require('inherits');
var duplexer = require('duplexer2');
var modes = require('./modes');
var ebtk = require('./EVP_BytesToKey');
var xor = require('./xor');
inherits(Splitter, Transform);
inherits(Cipher, Transform);
function Cipher(padding, mode, key, iv) {
if (!(this instanceof Cipher)) {
return new Cipher(padding, mode, key, iv);
}
Transform.call(this);
this._cache = new Splitter(padding);
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
}
Cipher.prototype._transform = function (data, _, next) {
this._cache.add(data);
var chunk;
var thing;
while ((chunk = this._cache.get())) {
thing = this._mode.encrypt(this, chunk);
this.push(thing);
}
next();
};
Cipher.prototype._flush = function (next) {
var chunk = this._cache.flush();
this.push(this._mode.encrypt(this, chunk));
this._cipher.scrub();
next();
};
Cipher.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]);
}
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
};
Cipher.prototype.final = function (outputEnc) {
this.end();
var outData = new Buffer('');
var chunk;
while ((chunk = this.read())) {
outData = Buffer.concat([outData, chunk]);
}
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
};
function Splitter(padding) {
if (!(this instanceof Splitter)) {
if (!(this instanceof Splitter)) {
return new Splitter(padding);

@@ -18,24 +69,19 @@ }

}
Transform.call(this);
this.cache = new Buffer('');
}
Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data]);
};
Splitter.prototype._transform = function (data, _, next) {
this.cache = Buffer.concat([this.cache, data]);
var i = 0;
var len = this.cache.length;
while (i + 15 < len) {
this.push(this.cache.slice(i, i + 16));
i += 16;
Splitter.prototype.get = function () {
if (this.cache.length > 15) {
var out = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
return out;
}
if (i) {
this.cache = this.cache.slice(i);
}
next();
return null;
};
Splitter.prototype._flush = function (next) {
Splitter.prototype.flush = function () {
if (!this._padding) {
this.push(this.cache);
return next();
return this.cache;
}

@@ -50,122 +96,11 @@ var len = 16 - this.cache.length;

var out = Buffer.concat([this.cache, padBuff]);
this.push(out);
next();
return out;
};
inherits(ECB, Transform);
function ECB(key) {
if (!(this instanceof ECB)) {
return new ECB(key);
}
Transform.call(this);
this._cipher = new aes.AES(key);
}
ECB.prototype._transform = function (data, _, next) {
var out = this._cipher.encryptBlock(data);
next(null, out);
var modelist = {
ECB: require('./modes/ecb'),
CBC: require('./modes/cbc'),
CFB: require('./modes/cfb'),
OFB: require('./modes/ofb'),
CTR: require('./modes/ctr')
};
ECB.prototype._flush = function (next) {
this._cipher.scrub();
next();
};
inherits(CBC, Transform);
function CBC(key, iv) {
if (!(this instanceof CBC)) {
return new CBC(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
}
CBC.prototype._transform = function (data, _, next) {
data = xor(data, this._prev);
this._prev = this._cipher.encryptBlock(data);
next(null, this._prev);
};
CBC.prototype._flush = function (next) {
this._cipher.scrub();
next();
};
inherits(CFB, Transform);
function CFB(key, iv) {
if (!(this instanceof CFB)) {
return new CFB(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
}
CFB.prototype._transform = function (data, _, next) {
var pad = this._cipher.encryptBlock(this._prev);
this._prev = xor(data, pad);
next(null, this._prev);
};
CFB.prototype._flush = function (next) {
this._cipher.scrub();
next();
};
inherits(OFB, Transform);
function OFB(key, iv) {
if (!(this instanceof OFB)) {
return new OFB(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = iv;
}
OFB.prototype._transform = function (data, _, next) {
this._prev = this._cipher.encryptBlock(this._prev);
next(null, xor(data, this._prev));
};
OFB.prototype._flush = function (next) {
this._cipher.scrub();
next();
};
inherits(CTR, Transform);
function CTR(key, iv) {
if (!(this instanceof CTR)) {
return new CTR(key, iv);
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._iv = new Buffer(iv.length);
iv.copy(this._iv);
}
CTR.prototype._transform = function (data, _, next) {
this.push(xor(data, this._cipher.encryptBlock(this._iv)));
this._incr32();
next();
};
CTR.prototype._flush = function (next) {
this._cipher.scrub();
this._iv.fill(0);
next();
};
CTR.prototype._incr32 = function () {
var len = this._iv.length;
var item;
while (len--) {
item = this._iv.readUInt8(len);
if (item === 255) {
this._iv.writeUInt8(0, len);
} else {
item++;
this._iv.writeUInt8(item, len);
break;
}
}
};
var modeStreams = {
ECB: ECB,
CBC: CBC,
CFB: CFB,
OFB: OFB,
CTR: CTR
};
module.exports = function (crypto) {

@@ -189,44 +124,3 @@ function createCipheriv(suite, password, iv) {

}
var splitter = new Splitter(config.padding);
var stream = new modeStreams[config.mode](password, iv);
splitter.on('data', function (d) {
stream.write(d);
});
splitter.on('finish', function () {
stream.end();
});
var out = duplexer(splitter, stream);
out.setAutoPadding = function (padding) {
splitter._padding = padding;
};
out._legacy = false;
var outData = new Buffer('');
out.update = function (data, inputEnd, outputEnc) {
if (out._legacy === false) {
out._legacy = true;
stream.on('data', function (chunk) {
outData = Buffer.concat([outData, chunk]);
});
stream.pause = function (){
// else it will stall out
};
}
splitter.write(data, inputEnd);
var ourData = outData;
outData = new Buffer('');
if (outputEnc) {
ourData = ourData.toString(outputEnc);
}
return ourData;
};
out.final = function (outputEnc) {
splitter.end();
var ourData = outData;
outData = null;
if (outputEnc) {
ourData = ourData.toString(outputEnc);
}
return ourData;
};
return out;
return new Cipher(config.padding, modelist[config.mode], password, iv);
}

@@ -233,0 +127,0 @@ function createCipher (suite, password) {

{
"name": "browserify-aes",
"version": "0.2.1",
"version": "0.2.2",
"description": "aes, for browserify",

@@ -28,4 +28,3 @@ "main": "index.js",

"dependencies": {
"inherits": "^2.0.1",
"duplexer2": "0.0.2"
"inherits": "^2.0.1"
},

@@ -32,0 +31,0 @@ "devDependencies": {

@@ -1,22 +0,34 @@

var types = ['aes-128-ctr','aes-192-ctr','aes-256-ctr'];
var modes = require('./modes');
var types = Object.keys(modes);
var fixtures = require('./test/fixtures.json');
var crypto = require('crypto');
var modes = require('./modes');
var ebtk = require('./EVP_BytesToKey');
var fs = require('fs');
fixtures.forEach(function (fixture) {
//var ciphers = fixture.results.ciphers = {};
types.forEach(function (cipher) {
var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
buf = Buffer.concat([buf, suite.final()]);
fixture.results.ciphers[cipher] = buf.toString('hex');
var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
var buf2 = new Buffer('');
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
buf2 = Buffer.concat([buf2, suite2.final()]);
fixture.results.cipherivs[cipher] = buf2.toString('hex');
});
});
var fixture = {
text: 'Chapter 24\n\nMy present situation was one in which all voluntary thought was\nswallowed up and lost. I was hurried away by fury; revenge alone\nendowed me with strength and composure; it moulded my feelings and\nallowed me to be calculating and calm at periods when otherwise\ndelirium or death would have been my portion.\n\nMy first resolution was to quit Geneva forever; my country, which, when\nI was happy and beloved, was dear to me, now, in my adversity, became\nhateful. I provided myself with a sum of money, together with a few\njewels which had belonged to my mother, and departed. And now my\nwanderings began which are to cease but with life. I have traversed a\nvast portion of the earth and have endured all the hardships which\ntravellers in deserts and barbarous countries are wont to meet. How I\nhave lived I hardly know; many times have I stretched my failing limbs\nupon the sandy plain and prayed for death. But revenge kept me alive;\nI dared not die and leave my adversary in being.\n\nWhen I quitted Geneva my first labour was to gain some clue by which I\nmight trace the steps of my fiendish enemy. But my plan was unsettled,\nand I wandered many hours round the confines of the town, uncertain\nwhat path I should pursue. As night approached I found myself at the\nentrance of the cemetery where William, Elizabeth, and my father\nreposed. I entered it and approached the tomb which marked their\ngraves. Everything was silent except the leaves of the trees, which\nwere gently agitated by the wind; the night was nearly dark, and the\nscene would have been solemn and affecting even to an uninterested\nobserver. The spirits of the departed seemed to flit around and to\ncast a shadow, which was felt but not seen, around the head of the\nmourner.\n\nThe deep grief which this scene had at first excited quickly gave way\nto rage and despair. They were dead, and I lived; their murderer also\nlived, and to destroy him I must drag out my weary existence. I knelt\non the grass and kissed the earth and with quivering lips exclaimed,\n"By the sacred earth on which I kneel, by the shades that wander near\nme, by the deep and eternal grief that I feel, I swear; and by thee, O\nNight, and the spirits that preside over thee, to pursue the daemon who\ncaused this misery, until he or I shall perish in mortal conflict. For\nthis purpose I will preserve my life; to execute this dear revenge will\nI again behold the sun and tread the green herbage of earth, which\notherwise should vanish from my eyes forever. And I call on you,\nspirits of the dead, and on you, wandering ministers of vengeance, to\naid and conduct me in my work. Let the cursed and hellish monster\ndrink deep of agony; let him feel the despair that now torments me." I\nhad begun my adjuration with solemnity and an awe which almost assured\nme that the shades of my murdered friends heard and approved my\ndevotion, but the furies possessed me as I concluded, and rage choked\nmy utterance.',
password: 'correcthorsebatterystaple',
iv: 'fffffffffffffffffffffffffffffffa',
results:{
ciphers: {},
cipherivs: {}
}
};
fixtures.push(fixture);
types.forEach(function (cipher) {
var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
buf = Buffer.concat([buf, suite.final()]);
fixture.results.ciphers[cipher] = buf.toString('hex');
if (modes[cipher].mode === 'ECB') {
return;
}
var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
var buf2 = new Buffer('');
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
buf2 = Buffer.concat([buf2, suite2.final()]);
fixture.results.cipherivs[cipher] = buf2.toString('hex');
});
fs.writeFileSync('./test/fixturesNew.json', JSON.stringify(fixtures, false, 4));

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc