browserify-aes
Advanced tools
Comparing version 0.3.0 to 0.4.0
var aes = require('./aes'); | ||
var Transform = require('stream').Transform; | ||
var Transform = require('./cipherBase'); | ||
var inherits = require('inherits'); | ||
var modes = require('./modes'); | ||
var StreamCipher = require('./streamCipher'); | ||
var ebtk = require('./EVP_BytesToKey'); | ||
inherits(Decipher, Transform); | ||
function Decipher(padding, mode, key, iv) { | ||
function Decipher(mode, key, iv) { | ||
if (!(this instanceof Decipher)) { | ||
return new Decipher(padding, mode, key, iv); | ||
return new Decipher(mode, key, iv); | ||
} | ||
Transform.call(this); | ||
this._cache = new Splitter(); | ||
if (padding === false) { | ||
this._padding = false; | ||
} else { | ||
this._padding = true; | ||
} | ||
this._last = void 0; | ||
@@ -40,33 +36,7 @@ this._cipher = new aes.AES(key); | ||
} | ||
if (this._padding) { | ||
this.push(unpad(this._mode.decrypt(this, chunk))); | ||
} else { | ||
this.push(this._mode.decrypt(this, chunk)); | ||
} | ||
this.push(unpad(this._mode.decrypt(this, chunk))); | ||
next(); | ||
}; | ||
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]); | ||
} | ||
if (outputEnc) { | ||
outData = outData.toString(outputEnc); | ||
} | ||
return outData; | ||
}; | ||
Decipher.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; | ||
}; | ||
@@ -130,3 +100,6 @@ function Splitter() { | ||
} | ||
return new Decipher(config.padding, modelist[config.mode], password, iv); | ||
if (config.type === 'stream') { | ||
return new StreamCipher(modelist[config.mode], password, iv, true); | ||
} | ||
return new Decipher(modelist[config.mode], password, iv); | ||
} | ||
@@ -133,0 +106,0 @@ |
var aes = require('./aes'); | ||
var Transform = require('stream').Transform; | ||
var Transform = require('./cipherBase'); | ||
var inherits = require('inherits'); | ||
var modes = require('./modes'); | ||
var ebtk = require('./EVP_BytesToKey'); | ||
var StreamCipher = require('./streamCipher'); | ||
inherits(Cipher, Transform); | ||
function Cipher(padding, mode, key, iv) { | ||
function Cipher(mode, key, iv) { | ||
if (!(this instanceof Cipher)) { | ||
return new Cipher(padding, mode, key, iv); | ||
return new Cipher(mode, key, iv); | ||
} | ||
Transform.call(this); | ||
this._cache = new Splitter(padding); | ||
this._cache = new Splitter(); | ||
this._cipher = new aes.AES(key); | ||
@@ -35,36 +36,7 @@ this._prev = new Buffer(iv.length); | ||
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) { | ||
function Splitter() { | ||
if (!(this instanceof Splitter)) { | ||
return new Splitter(padding); | ||
return new Splitter(); | ||
} | ||
if (padding === false) { | ||
this._padding = false; | ||
} else { | ||
this._padding = true; | ||
} | ||
this.cache = new Buffer(''); | ||
@@ -85,5 +57,2 @@ } | ||
Splitter.prototype.flush = function () { | ||
if (!this._padding) { | ||
return this.cache; | ||
} | ||
var len = 16 - this.cache.length; | ||
@@ -124,3 +93,6 @@ var padBuff = new Buffer(len); | ||
} | ||
return new Cipher(config.padding, modelist[config.mode], password, iv); | ||
if (config.type === 'stream') { | ||
return new StreamCipher(modelist[config.mode], password, iv); | ||
} | ||
return new Cipher(modelist[config.mode], password, iv); | ||
} | ||
@@ -127,0 +99,0 @@ function createCipher (suite, password) { |
36
modes.js
@@ -5,3 +5,4 @@ exports['aes-128-ecb'] = { | ||
iv: 0, | ||
mode: 'ECB' | ||
mode: 'ECB', | ||
type: 'block' | ||
}; | ||
@@ -12,3 +13,4 @@ exports['aes-192-ecb'] = { | ||
iv: 0, | ||
mode: 'ECB' | ||
mode: 'ECB', | ||
type: 'block' | ||
}; | ||
@@ -19,3 +21,4 @@ exports['aes-256-ecb'] = { | ||
iv: 0, | ||
mode: 'ECB' | ||
mode: 'ECB', | ||
type: 'block' | ||
}; | ||
@@ -26,3 +29,4 @@ exports['aes-128-cbc'] = { | ||
iv: 16, | ||
mode: 'CBC' | ||
mode: 'CBC', | ||
type: 'block' | ||
}; | ||
@@ -33,3 +37,4 @@ exports['aes-192-cbc'] = { | ||
iv: 16, | ||
mode: 'CBC' | ||
mode: 'CBC', | ||
type: 'block' | ||
}; | ||
@@ -40,3 +45,4 @@ exports['aes-256-cbc'] = { | ||
iv: 16, | ||
mode: 'CBC' | ||
mode: 'CBC', | ||
type: 'block' | ||
}; | ||
@@ -51,3 +57,3 @@ exports['aes128'] = exports['aes-128-cbc']; | ||
mode: 'CFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -59,3 +65,3 @@ exports['aes-192-cfb'] = { | ||
mode: 'CFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -67,3 +73,3 @@ exports['aes-256-cfb'] = { | ||
mode: 'CFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -75,3 +81,3 @@ exports['aes-128-ofb'] = { | ||
mode: 'OFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -83,3 +89,3 @@ exports['aes-192-ofb'] = { | ||
mode: 'OFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -91,3 +97,3 @@ exports['aes-256-ofb'] = { | ||
mode: 'OFB', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -99,3 +105,3 @@ exports['aes-128-ctr'] = { | ||
mode: 'CTR', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -107,3 +113,3 @@ exports['aes-192-ctr'] = { | ||
mode: 'CTR', | ||
padding: false | ||
type: 'stream' | ||
}; | ||
@@ -115,3 +121,3 @@ exports['aes-256-ctr'] = { | ||
mode: 'CTR', | ||
padding: false | ||
type: 'stream' | ||
}; |
var xor = require('../xor'); | ||
exports.encrypt = function (self, block) { | ||
var pad = self._cipher.encryptBlock(self._prev); | ||
self._prev = xor(block, pad); | ||
return self._prev; | ||
exports.encrypt = function (self, data, decrypt) { | ||
var out = new Buffer(''); | ||
var len; | ||
while (data.length) { | ||
if (self._cache.length === 0) { | ||
self._cache = self._cipher.encryptBlock(self._prev); | ||
self._prev = new Buffer(''); | ||
} | ||
if (self._cache.length <= data.length) { | ||
len = self._cache.length; | ||
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)]); | ||
data = data.slice(len); | ||
} else { | ||
out = Buffer.concat([out, encryptStart(self, data, decrypt)]); | ||
break; | ||
} | ||
} | ||
return out; | ||
}; | ||
exports.decrypt = function (self, block) { | ||
// yes encrypt | ||
var pad = self._cipher.encryptBlock(self._prev); | ||
self._prev = block; | ||
return xor(pad, block); | ||
}; | ||
function encryptStart(self, data, decrypt) { | ||
var len = data.length; | ||
var out = xor(data, self._cache); | ||
self._cache = self._cache.slice(len); | ||
self._prev = Buffer.concat([self._prev, decrypt?data:out]); | ||
return out; | ||
} |
var xor = require('../xor'); | ||
exports.encrypt = exports.decrypt = function (self, block) { | ||
var out = xor(block, self._cipher.encryptBlock(self._prev)); | ||
function getBlock(self) { | ||
var out = self._cipher.encryptBlock(self._prev); | ||
incr32(self._prev); | ||
return out; | ||
} | ||
exports.encrypt = function (self, chunk) { | ||
while (self._cache.length < chunk.length) { | ||
self._cache = Buffer.concat([self._cache, getBlock(self)]); | ||
} | ||
var pad = self._cache.slice(0, chunk.length); | ||
self._cache = self._cache.slice(chunk.length); | ||
return xor(chunk, pad); | ||
}; | ||
@@ -7,0 +15,0 @@ function incr32(iv) { |
var xor = require('../xor'); | ||
exports.encrypt = exports.decrypt = function (self, block) { | ||
function getBlock(self) { | ||
self._prev = self._cipher.encryptBlock(self._prev); | ||
return xor(block, self._prev); | ||
return self._prev; | ||
} | ||
exports.encrypt = function (self, chunk) { | ||
while (self._cache.length < chunk.length) { | ||
self._cache = Buffer.concat([self._cache, getBlock(self)]); | ||
} | ||
var pad = self._cache.slice(0, chunk.length); | ||
self._cache = self._cache.slice(chunk.length); | ||
return xor(chunk, pad); | ||
}; |
{ | ||
"name": "browserify-aes", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "aes, for browserify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,18 +8,2 @@ var test = require('tape'); | ||
var ebtk = require('../EVP_BytesToKey'); | ||
function decriptNoPadding(cipher, password, thing, code) { | ||
var suite = _crypto.createDecipher(cipher, password); | ||
var buf = new Buffer(''); | ||
suite.on('data', function (d) { | ||
buf = Buffer.concat([buf, d]); | ||
}); | ||
suite.on('error', function (e) { | ||
console.log(e); | ||
}); | ||
suite.on("finish", function () { | ||
console.log(code, buf.toString('hex')); | ||
}); | ||
suite.setAutoPadding(false); | ||
suite.write(thing, 'hex'); | ||
suite.end(); | ||
} | ||
fixtures.forEach(function (fixture, i) { | ||
@@ -48,12 +32,18 @@ //var ciphers = fixture.results.ciphers = {}; | ||
test('fixture ' + i + ' ' + cipher + '-legacy', function (t) { | ||
t.plan(1); | ||
t.plan(3); | ||
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()]); | ||
var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password)); | ||
var buf2 = new Buffer(''); | ||
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]); | ||
var inbuf = new Buffer(fixture.text); | ||
var mid = ~~(inbuf.length/2); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate'); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2'); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
buf2 = Buffer.concat([buf2, suite2.final()]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex')); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final'); | ||
}); | ||
@@ -80,13 +70,19 @@ test('fixture ' + i + ' ' + cipher + '-decrypt', function (t) { | ||
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) { | ||
t.plan(2); | ||
t.plan(4); | ||
var suite = crypto.createDecipher(cipher, new Buffer(fixture.password)); | ||
var buf = new Buffer(''); | ||
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.results.ciphers[cipher], 'hex'))]); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password)); | ||
var buf2 = new Buffer(''); | ||
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.results.ciphers[cipher], 'hex'))]); | ||
var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex'); | ||
var mid = ~~(inbuf.length/2); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate'); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2'); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
buf2 = Buffer.concat([buf2, suite2.final()]); | ||
t.equals(buf.toString('utf8'), fixture.text); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8')); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final'); | ||
}); | ||
@@ -115,13 +111,19 @@ //var cipherivs = fixture.results.cipherivs = {}; | ||
test('fixture ' + i + ' ' + cipher + '-legacy-iv', function (t) { | ||
t.plan(2); | ||
t.plan(4); | ||
var suite = crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex')); | ||
var buf = new Buffer(''); | ||
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
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))]); | ||
var inbuf = new Buffer(fixture.text); | ||
var mid = ~~(inbuf.length/2); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate'); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2'); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
buf2 = Buffer.concat([buf2, suite2.final()]); | ||
t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher]); | ||
t.equals(buf.toString('hex'), buf2.toString('hex')); | ||
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final'); | ||
}); | ||
@@ -145,13 +147,19 @@ test('fixture ' + i + ' ' + cipher + '-iv-decrypt', function (t) { | ||
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) { | ||
t.plan(2); | ||
t.plan(4); | ||
var suite = crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex')); | ||
var buf = new Buffer(''); | ||
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.results.cipherivs[cipher], 'hex'))]); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
var suite2 = _crypto.createDecipheriv(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.results.cipherivs[cipher], 'hex'))]); | ||
var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex'); | ||
var mid = ~~(inbuf.length/2); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate'); | ||
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]); | ||
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2'); | ||
buf = Buffer.concat([buf, suite.final()]); | ||
buf2 = Buffer.concat([buf2, suite2.final()]); | ||
t.equals(buf.toString('utf8'), fixture.text); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8')); | ||
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final'); | ||
}); | ||
@@ -158,0 +166,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
285917
22
2124