react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-aes / encrypter.js
80540 viewsvar aes = require('./aes')1var Transform = require('./cipherBase')2var inherits = require('inherits')3var modes = require('./modes')4var ebtk = require('./EVP_BytesToKey')5var StreamCipher = require('./streamCipher')6var AuthCipher = require('./authCipher')7inherits(Cipher, Transform)8function Cipher (mode, key, iv) {9if (!(this instanceof Cipher)) {10return new Cipher(mode, key, iv)11}12Transform.call(this)13this._cache = new Splitter()14this._cipher = new aes.AES(key)15this._prev = new Buffer(iv.length)16iv.copy(this._prev)17this._mode = mode18this._autopadding = true19}20Cipher.prototype._update = function (data) {21this._cache.add(data)22var chunk23var thing24var out = []25while ((chunk = this._cache.get())) {26thing = this._mode.encrypt(this, chunk)27out.push(thing)28}29return Buffer.concat(out)30}31Cipher.prototype._final = function () {32var chunk = this._cache.flush()33if (this._autopadding) {34chunk = this._mode.encrypt(this, chunk)35this._cipher.scrub()36return chunk37} else if (chunk.toString('hex') !== '10101010101010101010101010101010') {38this._cipher.scrub()39throw new Error('data not multiple of block length')40}41}42Cipher.prototype.setAutoPadding = function (setTo) {43this._autopadding = !!setTo44}4546function Splitter () {47if (!(this instanceof Splitter)) {48return new Splitter()49}50this.cache = new Buffer('')51}52Splitter.prototype.add = function (data) {53this.cache = Buffer.concat([this.cache, data])54}5556Splitter.prototype.get = function () {57if (this.cache.length > 15) {58var out = this.cache.slice(0, 16)59this.cache = this.cache.slice(16)60return out61}62return null63}64Splitter.prototype.flush = function () {65var len = 16 - this.cache.length66var padBuff = new Buffer(len)6768var i = -169while (++i < len) {70padBuff.writeUInt8(len, i)71}72var out = Buffer.concat([this.cache, padBuff])73return out74}75var modelist = {76ECB: require('./modes/ecb'),77CBC: require('./modes/cbc'),78CFB: require('./modes/cfb'),79CFB8: require('./modes/cfb8'),80CFB1: require('./modes/cfb1'),81OFB: require('./modes/ofb'),82CTR: require('./modes/ctr'),83GCM: require('./modes/ctr')84}8586function createCipheriv (suite, password, iv) {87var config = modes[suite.toLowerCase()]88if (!config) {89throw new TypeError('invalid suite type')90}91if (typeof iv === 'string') {92iv = new Buffer(iv)93}94if (typeof password === 'string') {95password = new Buffer(password)96}97if (password.length !== config.key / 8) {98throw new TypeError('invalid key length ' + password.length)99}100if (iv.length !== config.iv) {101throw new TypeError('invalid iv length ' + iv.length)102}103if (config.type === 'stream') {104return new StreamCipher(modelist[config.mode], password, iv)105} else if (config.type === 'auth') {106return new AuthCipher(modelist[config.mode], password, iv)107}108return new Cipher(modelist[config.mode], password, iv)109}110function createCipher (suite, password) {111var config = modes[suite.toLowerCase()]112if (!config) {113throw new TypeError('invalid suite type')114}115var keys = ebtk(password, config.key, config.iv)116return createCipheriv(suite, keys.key, keys.iv)117}118119exports.createCipheriv = createCipheriv120exports.createCipher = createCipher121122123