react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-aes / decrypter.js
80542 viewsvar aes = require('./aes')1var Transform = require('./cipherBase')2var inherits = require('inherits')3var modes = require('./modes')4var StreamCipher = require('./streamCipher')5var AuthCipher = require('./authCipher')6var ebtk = require('./EVP_BytesToKey')78inherits(Decipher, Transform)9function Decipher (mode, key, iv) {10if (!(this instanceof Decipher)) {11return new Decipher(mode, key, iv)12}13Transform.call(this)14this._cache = new Splitter()15this._last = void 016this._cipher = new aes.AES(key)17this._prev = new Buffer(iv.length)18iv.copy(this._prev)19this._mode = mode20this._autopadding = true21}22Decipher.prototype._update = function (data) {23this._cache.add(data)24var chunk25var thing26var out = []27while ((chunk = this._cache.get(this._autopadding))) {28thing = this._mode.decrypt(this, chunk)29out.push(thing)30}31return Buffer.concat(out)32}33Decipher.prototype._final = function () {34var chunk = this._cache.flush()35if (this._autopadding) {36return unpad(this._mode.decrypt(this, chunk))37} else if (chunk) {38throw new Error('data not multiple of block length')39}40}41Decipher.prototype.setAutoPadding = function (setTo) {42this._autopadding = !!setTo43}44function Splitter () {45if (!(this instanceof Splitter)) {46return new Splitter()47}48this.cache = new Buffer('')49}50Splitter.prototype.add = function (data) {51this.cache = Buffer.concat([this.cache, data])52}5354Splitter.prototype.get = function (autoPadding) {55var out56if (autoPadding) {57if (this.cache.length > 16) {58out = this.cache.slice(0, 16)59this.cache = this.cache.slice(16)60return out61}62} else {63if (this.cache.length >= 16) {64out = this.cache.slice(0, 16)65this.cache = this.cache.slice(16)66return out67}68}69return null70}71Splitter.prototype.flush = function () {72if (this.cache.length) {73return this.cache74}75}76function unpad (last) {77var padded = last[15]78var i = -179while (++i < padded) {80if (last[(i + (16 - padded))] !== padded) {81throw new Error('unable to decrypt data')82}83}84if (padded === 16) {85return86}87return last.slice(0, 16 - padded)88}8990var modelist = {91ECB: require('./modes/ecb'),92CBC: require('./modes/cbc'),93CFB: require('./modes/cfb'),94CFB8: require('./modes/cfb8'),95CFB1: require('./modes/cfb1'),96OFB: require('./modes/ofb'),97CTR: require('./modes/ctr'),98GCM: require('./modes/ctr')99}100101function createDecipheriv (suite, password, iv) {102var config = modes[suite.toLowerCase()]103if (!config) {104throw new TypeError('invalid suite type')105}106if (typeof iv === 'string') {107iv = new Buffer(iv)108}109if (typeof password === 'string') {110password = new Buffer(password)111}112if (password.length !== config.key / 8) {113throw new TypeError('invalid key length ' + password.length)114}115if (iv.length !== config.iv) {116throw new TypeError('invalid iv length ' + iv.length)117}118if (config.type === 'stream') {119return new StreamCipher(modelist[config.mode], password, iv, true)120} else if (config.type === 'auth') {121return new AuthCipher(modelist[config.mode], password, iv, true)122}123return new Decipher(modelist[config.mode], password, iv)124}125126function createDecipher (suite, password) {127var config = modes[suite.toLowerCase()]128if (!config) {129throw new TypeError('invalid suite type')130}131var keys = ebtk(password, config.key, config.iv)132return createDecipheriv(suite, keys.key, keys.iv)133}134exports.createDecipher = createDecipher135exports.createDecipheriv = createDecipheriv136137138