aboutsummaryrefslogtreecommitdiff
path: root/node_modules/browserify-aes/decrypter.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/browserify-aes/decrypter.js')
-rw-r--r--node_modules/browserify-aes/decrypter.js94
1 files changed, 39 insertions, 55 deletions
diff --git a/node_modules/browserify-aes/decrypter.js b/node_modules/browserify-aes/decrypter.js
index b7b8bb02b..d752033e9 100644
--- a/node_modules/browserify-aes/decrypter.js
+++ b/node_modules/browserify-aes/decrypter.js
@@ -1,25 +1,25 @@
-var aes = require('./aes')
-var Transform = require('cipher-base')
-var inherits = require('inherits')
-var modes = require('./modes')
-var StreamCipher = require('./streamCipher')
var AuthCipher = require('./authCipher')
+var Buffer = require('safe-buffer').Buffer
+var MODES = require('./modes')
+var StreamCipher = require('./streamCipher')
+var Transform = require('cipher-base')
+var aes = require('./aes')
var ebtk = require('evp_bytestokey')
+var inherits = require('inherits')
-inherits(Decipher, Transform)
function Decipher (mode, key, iv) {
- if (!(this instanceof Decipher)) {
- return new Decipher(mode, key, iv)
- }
Transform.call(this)
+
this._cache = new Splitter()
this._last = void 0
this._cipher = new aes.AES(key)
- this._prev = new Buffer(iv.length)
- iv.copy(this._prev)
+ this._prev = Buffer.from(iv)
this._mode = mode
this._autopadding = true
}
+
+inherits(Decipher, Transform)
+
Decipher.prototype._update = function (data) {
this._cache.add(data)
var chunk
@@ -31,6 +31,7 @@ Decipher.prototype._update = function (data) {
}
return Buffer.concat(out)
}
+
Decipher.prototype._final = function () {
var chunk = this._cache.flush()
if (this._autopadding) {
@@ -39,16 +40,16 @@ Decipher.prototype._final = function () {
throw new Error('data not multiple of block length')
}
}
+
Decipher.prototype.setAutoPadding = function (setTo) {
this._autopadding = !!setTo
return this
}
+
function Splitter () {
- if (!(this instanceof Splitter)) {
- return new Splitter()
- }
- this.cache = new Buffer('')
+ this.cache = Buffer.allocUnsafe(0)
}
+
Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data])
}
@@ -68,13 +69,14 @@ Splitter.prototype.get = function (autoPadding) {
return out
}
}
+
return null
}
+
Splitter.prototype.flush = function () {
- if (this.cache.length) {
- return this.cache
- }
+ if (this.cache.length) return this.cache
}
+
function unpad (last) {
var padded = last[15]
var i = -1
@@ -83,55 +85,37 @@ function unpad (last) {
throw new Error('unable to decrypt data')
}
}
- if (padded === 16) {
- return
- }
- return last.slice(0, 16 - padded)
-}
+ if (padded === 16) return
-var modelist = {
- ECB: require('./modes/ecb'),
- CBC: require('./modes/cbc'),
- CFB: require('./modes/cfb'),
- CFB8: require('./modes/cfb8'),
- CFB1: require('./modes/cfb1'),
- OFB: require('./modes/ofb'),
- CTR: require('./modes/ctr'),
- GCM: require('./modes/ctr')
+ return last.slice(0, 16 - padded)
}
function createDecipheriv (suite, password, iv) {
- var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
- if (typeof iv === 'string') {
- iv = new Buffer(iv)
- }
- if (typeof password === 'string') {
- password = new Buffer(password)
- }
- if (password.length !== config.key / 8) {
- throw new TypeError('invalid key length ' + password.length)
- }
- if (iv.length !== config.iv) {
- throw new TypeError('invalid iv length ' + iv.length)
- }
+ var config = MODES[suite.toLowerCase()]
+ if (!config) throw new TypeError('invalid suite type')
+
+ if (typeof iv === 'string') iv = Buffer.from(iv)
+ if (config.mode !== 'GCM' && iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length)
+
+ if (typeof password === 'string') password = Buffer.from(password)
+ if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
+
if (config.type === 'stream') {
- return new StreamCipher(modelist[config.mode], password, iv, true)
+ return new StreamCipher(config.module, password, iv, true)
} else if (config.type === 'auth') {
- return new AuthCipher(modelist[config.mode], password, iv, true)
+ return new AuthCipher(config.module, password, iv, true)
}
- return new Decipher(modelist[config.mode], password, iv)
+
+ return new Decipher(config.module, password, iv)
}
function createDecipher (suite, password) {
- var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
+ var config = MODES[suite.toLowerCase()]
+ if (!config) throw new TypeError('invalid suite type')
+
var keys = ebtk(password, false, config.key, config.iv)
return createDecipheriv(suite, keys.key, keys.iv)
}
+
exports.createDecipher = createDecipher
exports.createDecipheriv = createDecipheriv