var forge=require("./forge");require("./util"),module.exports=forge.cipher=forge.cipher||{},forge.cipher.algorithms=forge.cipher.algorithms||{},forge.cipher.createCipher=function(t,i){var e=t;if("string"==typeof e&&(e=forge.cipher.getAlgorithm(e))&&(e=e()),!e)throw Error("Unsupported algorithm: "+t);return new forge.cipher.BlockCipher({algorithm:e,key:i,decrypt:!1})},forge.cipher.createDecipher=function(t,i){var e=t;if("string"==typeof e&&(e=forge.cipher.getAlgorithm(e))&&(e=e()),!e)throw Error("Unsupported algorithm: "+t);return new forge.cipher.BlockCipher({algorithm:e,key:i,decrypt:!0})},forge.cipher.registerAlgorithm=function(t,i){t=t.toUpperCase(),forge.cipher.algorithms[t]=i},forge.cipher.getAlgorithm=function(t){return(t=t.toUpperCase())in forge.cipher.algorithms?forge.cipher.algorithms[t]:null};var BlockCipher=forge.cipher.BlockCipher=function(t){this.algorithm=t.algorithm,this.mode=this.algorithm.mode,this.blockSize=this.mode.blockSize,this._finish=!1,this._input=null,this.output=null,this._op=t.decrypt?this.mode.decrypt:this.mode.encrypt,this._decrypt=t.decrypt,this.algorithm.initialize(t)};BlockCipher.prototype.start=function(t){var i={};for(var e in t=t||{})i[e]=t[e];i.decrypt=this._decrypt,this._finish=!1,this._input=forge.util.createBuffer(),this.output=t.output||forge.util.createBuffer(),this.mode.start(i)},BlockCipher.prototype.update=function(t){for(t&&this._input.putBuffer(t);!this._op.call(this.mode,this._input,this.output,this._finish)&&!this._finish;);this._input.compact()},BlockCipher.prototype.finish=function(t){t&&("ECB"===this.mode.name||"CBC"===this.mode.name)&&(this.mode.pad=function(i){return t(this.blockSize,i,!1)},this.mode.unpad=function(i){return t(this.blockSize,i,!0)});var i={};return i.decrypt=this._decrypt,i.overflow=this._input.length()%this.blockSize,(!!this._decrypt||!this.mode.pad||!!this.mode.pad(this._input,i))&&(this._finish=!0,this.update(),(!this._decrypt||!this.mode.unpad||!!this.mode.unpad(this.output,i))&&(!this.mode.afterFinish||!!this.mode.afterFinish(this.output,i)))};