hybrid-crypto-js
Version:
Hybrid (RSA+AES) encryption and decryption toolkit for JavaScript
1 lines • 394 kB
JavaScript
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){"use strict";module.exports={AES_STANDARD:"AES-CBC",RSA_STANDARD:"RSA-OAEP",DEFAULT_MESSAGE_DIGEST:"sha256",DEFAULT_AES_KEY_SIZE:256,DEFAULT_AES_IV_SIZE:32}},{}],2:[function(require,module,exports){"use strict";function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable});keys.push.apply(keys,symbols)}return keys}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){_defineProperty(target,key,source[key])})}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source))}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))})}}return target}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}var helpers=require("./helpers");var forge=require("node-forge");var pki=forge.pki,rsa=forge.rsa;var _require=require("./constants"),DEFAULT_MESSAGE_DIGEST=_require.DEFAULT_MESSAGE_DIGEST,DEFAULT_AES_KEY_SIZE=_require.DEFAULT_AES_KEY_SIZE,DEFAULT_AES_IV_SIZE=_require.DEFAULT_AES_IV_SIZE,AES_STANDARD=_require.AES_STANDARD,RSA_STANDARD=_require.RSA_STANDARD;var Crypt=function(){function Crypt(){var options=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};_classCallCheck(this,Crypt);this.options=_objectSpread({md:DEFAULT_MESSAGE_DIGEST,aesKeySize:DEFAULT_AES_KEY_SIZE,aesIvSize:DEFAULT_AES_IV_SIZE,aesStandard:AES_STANDARD,rsaStandard:RSA_STANDARD,entropy:undefined},options);if(this.options.entropy){this._entropy(this.options.entropy)}}_createClass(Crypt,[{key:"_getMessageDigest",value:function _getMessageDigest(messageDigest){switch(messageDigest){case"sha1":return forge.md.sha1.create();case"sha256":return forge.md.sha256.create();case"sha384":return forge.md.sha384.create();case"sha512":return forge.md.sha512.create();case"md5":return forge.md.md5.create();default:console.warn('Message digest "'.concat(this.options.md,'" not found. Using default message digest "sha1" instead'));return forge.md.sha1.create()}}},{key:"_parseSignature",value:function _parseSignature(_signature){try{return JSON.parse(_signature)}catch(e){return{signature:_signature,md:"sha1",v:helpers.version()}}}},{key:"fingerprint",value:function fingerprint(publicKey){return pki.getPublicKeyFingerprint(publicKey,{encoding:"hex",delimiter:":"})}},{key:"signature",value:function signature(privateKey,message){var checkSum=this._getMessageDigest(this.options.md);checkSum.update(message,"utf8");if(typeof privateKey==="string")privateKey=pki.privateKeyFromPem(privateKey);var signature=privateKey.sign(checkSum);var signature64=forge.util.encode64(signature);return JSON.stringify({signature:signature64,md:this.options.md})}},{key:"verify",value:function verify(publicKey,_signature,decrypted){if(!_signature)return false;var _this$_parseSignature=this._parseSignature(_signature),signature=_this$_parseSignature.signature,md=_this$_parseSignature.md;var checkSum=this._getMessageDigest(md);checkSum.update(decrypted,"utf8");signature=forge.util.decode64(signature);if(typeof publicKey==="string")publicKey=pki.publicKeyFromPem(publicKey);return publicKey.verify(checkSum.digest().getBytes(),signature)}},{key:"encrypt",value:function encrypt(publicKeys,message,signature){var _this=this;publicKeys=helpers.toArray(publicKeys);publicKeys=publicKeys.map(function(key){return typeof key==="string"?pki.publicKeyFromPem(key):key});var iv=forge.random.getBytesSync(this.options.aesIvSize);var key=forge.random.getBytesSync(this.options.aesKeySize/8);var encryptedKeys={};publicKeys.forEach(function(publicKey){var encryptedKey=publicKey.encrypt(key,_this.options.rsaStandard);var fingerprint=_this.fingerprint(publicKey);encryptedKeys[fingerprint]=forge.util.encode64(encryptedKey)});var buffer=forge.util.createBuffer(message,"utf8");var cipher=forge.cipher.createCipher(this.options.aesStandard,key);cipher.start({iv:iv});cipher.update(buffer);cipher.finish();var payload={};payload.v=helpers.version();payload.iv=forge.util.encode64(iv);payload.keys=encryptedKeys;payload.cipher=forge.util.encode64(cipher.output.data);payload.signature=signature;payload.tag=cipher.mode.tag&&forge.util.encode64(cipher.mode.tag.getBytes());return JSON.stringify(payload)}},{key:"decrypt",value:function decrypt(privateKey,encrypted){this._validate(encrypted);var payload=JSON.parse(encrypted);if(typeof privateKey==="string")privateKey=pki.privateKeyFromPem(privateKey);var fingerprint=this.fingerprint(privateKey);var encryptedKey=payload.keys[fingerprint];if(!encryptedKey)throw"RSA fingerprint doesn't match with any of the encrypted message's fingerprints";var keyBytes=forge.util.decode64(encryptedKey);var iv=forge.util.decode64(payload.iv);var cipher=forge.util.decode64(payload.cipher);var tag=payload.tag&&forge.util.decode64(payload.tag);var key=privateKey.decrypt(keyBytes,this.options.rsaStandard);var buffer=forge.util.createBuffer(cipher);var decipher=forge.cipher.createDecipher(this.options.aesStandard,key);decipher.start({iv:iv,tag:tag});decipher.update(buffer);decipher.finish();var bytes=decipher.output.getBytes();var decrypted=forge.util.decodeUtf8(bytes);var output={};output.message=decrypted;output.signature=payload.signature;return output}},{key:"_validate",value:function _validate(encrypted){var p=JSON.parse(encrypted);if(!(p.hasOwnProperty("v")&&p.hasOwnProperty("iv")&&p.hasOwnProperty("keys")&&p.hasOwnProperty("cipher")))throw"Encrypted message is not valid"}},{key:"_entropy",value:function _entropy(input){var inputString=String(input);var bytes=forge.util.encodeUtf8(inputString);forge.random.collect(bytes)}}]);return Crypt}();module.exports=Crypt},{"./constants":1,"./helpers":3,"node-forge":18}],3:[function(require,module,exports){"use strict";var pkg=require("../package.json");module.exports={version:function version(){return"".concat(pkg.name,"_").concat(pkg.version)},toArray:function toArray(obj){return Array.isArray(obj)?obj:[obj]}}},{"../package.json":52}],4:[function(require,module,exports){"use strict";function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable});keys.push.apply(keys,symbols)}return keys}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){_defineProperty(target,key,source[key])})}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source))}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))})}}return target}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}var forge=require("node-forge");var pki=forge.pki;var RSA=function(){function RSA(){var options=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};_classCallCheck(this,RSA);this.options=_objectSpread({keySize:4096,entropy:undefined},options);if(this.options.entropy){this._entropy(this.options.entropy)}}_createClass(RSA,[{key:"generateKeyPair",value:function generateKeyPair(callback,keySize){pki.rsa.generateKeyPair({bits:keySize||this.options.keySize,workers:-1},function(err,keyPair){keyPair.publicKey=pki.publicKeyToPem(keyPair.publicKey);keyPair.privateKey=pki.privateKeyToPem(keyPair.privateKey);callback(keyPair)})}},{key:"generateKeyPairAsync",value:function generateKeyPairAsync(keySize){var _this=this;return new Promise(function(resolve){_this.generateKeyPair(resolve,keySize)})}},{key:"_entropy",value:function _entropy(input){var inputString=String(input);var bytes=forge.util.encodeUtf8(inputString);forge.random.collect(bytes)}}]);return RSA}();module.exports=RSA},{"node-forge":18}],5:[function(require,module,exports){"use strict";var _crypt=_interopRequireDefault(require("./crypt"));var _rsa=_interopRequireDefault(require("./rsa"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}window.Crypt=_crypt.default;window.RSA=_rsa.default},{"./crypt":2,"./rsa":4}],6:[function(require,module,exports){},{}],7:[function(require,module,exports){var forge=require("./forge");require("./cipher");require("./cipherModes");require("./util");module.exports=forge.aes=forge.aes||{};forge.aes.startEncrypting=function(key,iv,output,mode){var cipher=_createCipher({key:key,output:output,decrypt:false,mode:mode});cipher.start(iv);return cipher};forge.aes.createEncryptionCipher=function(key,mode){return _createCipher({key:key,output:null,decrypt:false,mode:mode})};forge.aes.startDecrypting=function(key,iv,output,mode){var cipher=_createCipher({key:key,output:output,decrypt:true,mode:mode});cipher.start(iv);return cipher};forge.aes.createDecryptionCipher=function(key,mode){return _createCipher({key:key,output:null,decrypt:true,mode:mode})};forge.aes.Algorithm=function(name,mode){if(!init){initialize()}var self=this;self.name=name;self.mode=new mode({blockSize:16,cipher:{encrypt:function(inBlock,outBlock){return _updateBlock(self._w,inBlock,outBlock,false)},decrypt:function(inBlock,outBlock){return _updateBlock(self._w,inBlock,outBlock,true)}}});self._init=false};forge.aes.Algorithm.prototype.initialize=function(options){if(this._init){return}var key=options.key;var tmp;if(typeof key==="string"&&(key.length===16||key.length===24||key.length===32)){key=forge.util.createBuffer(key)}else if(forge.util.isArray(key)&&(key.length===16||key.length===24||key.length===32)){tmp=key;key=forge.util.createBuffer();for(var i=0;i<tmp.length;++i){key.putByte(tmp[i])}}if(!forge.util.isArray(key)){tmp=key;key=[];var len=tmp.length();if(len===16||len===24||len===32){len=len>>>2;for(var i=0;i<len;++i){key.push(tmp.getInt32())}}}if(!forge.util.isArray(key)||!(key.length===4||key.length===6||key.length===8)){throw new Error("Invalid key parameter.")}var mode=this.mode.name;var encryptOp=["CFB","OFB","CTR","GCM"].indexOf(mode)!==-1;this._w=_expandKey(key,options.decrypt&&!encryptOp);this._init=true};forge.aes._expandKey=function(key,decrypt){if(!init){initialize()}return _expandKey(key,decrypt)};forge.aes._updateBlock=_updateBlock;registerAlgorithm("AES-ECB",forge.cipher.modes.ecb);registerAlgorithm("AES-CBC",forge.cipher.modes.cbc);registerAlgorithm("AES-CFB",forge.cipher.modes.cfb);registerAlgorithm("AES-OFB",forge.cipher.modes.ofb);registerAlgorithm("AES-CTR",forge.cipher.modes.ctr);registerAlgorithm("AES-GCM",forge.cipher.modes.gcm);function registerAlgorithm(name,mode){var factory=function(){return new forge.aes.Algorithm(name,mode)};forge.cipher.registerAlgorithm(name,factory)}var init=false;var Nb=4;var sbox;var isbox;var rcon;var mix;var imix;function initialize(){init=true;rcon=[0,1,2,4,8,16,32,64,128,27,54];var xtime=new Array(256);for(var i=0;i<128;++i){xtime[i]=i<<1;xtime[i+128]=i+128<<1^283}sbox=new Array(256);isbox=new Array(256);mix=new Array(4);imix=new Array(4);for(var i=0;i<4;++i){mix[i]=new Array(256);imix[i]=new Array(256)}var e=0,ei=0,e2,e4,e8,sx,sx2,me,ime;for(var i=0;i<256;++i){sx=ei^ei<<1^ei<<2^ei<<3^ei<<4;sx=sx>>8^sx&255^99;sbox[e]=sx;isbox[sx]=e;sx2=xtime[sx];e2=xtime[e];e4=xtime[e2];e8=xtime[e4];me=sx2<<24^sx<<16^sx<<8^(sx^sx2);ime=(e2^e4^e8)<<24^(e^e8)<<16^(e^e4^e8)<<8^(e^e2^e8);for(var n=0;n<4;++n){mix[n][e]=me;imix[n][sx]=ime;me=me<<24|me>>>8;ime=ime<<24|ime>>>8}if(e===0){e=ei=1}else{e=e2^xtime[xtime[xtime[e2^e8]]];ei^=xtime[xtime[ei]]}}}function _expandKey(key,decrypt){var w=key.slice(0);var temp,iNk=1;var Nk=w.length;var Nr1=Nk+6+1;var end=Nb*Nr1;for(var i=Nk;i<end;++i){temp=w[i-1];if(i%Nk===0){temp=sbox[temp>>>16&255]<<24^sbox[temp>>>8&255]<<16^sbox[temp&255]<<8^sbox[temp>>>24]^rcon[iNk]<<24;iNk++}else if(Nk>6&&i%Nk===4){temp=sbox[temp>>>24]<<24^sbox[temp>>>16&255]<<16^sbox[temp>>>8&255]<<8^sbox[temp&255]}w[i]=w[i-Nk]^temp}if(decrypt){var tmp;var m0=imix[0];var m1=imix[1];var m2=imix[2];var m3=imix[3];var wnew=w.slice(0);end=w.length;for(var i=0,wi=end-Nb;i<end;i+=Nb,wi-=Nb){if(i===0||i===end-Nb){wnew[i]=w[wi];wnew[i+1]=w[wi+3];wnew[i+2]=w[wi+2];wnew[i+3]=w[wi+1]}else{for(var n=0;n<Nb;++n){tmp=w[wi+n];wnew[i+(3&-n)]=m0[sbox[tmp>>>24]]^m1[sbox[tmp>>>16&255]]^m2[sbox[tmp>>>8&255]]^m3[sbox[tmp&255]]}}}w=wnew}return w}function _updateBlock(w,input,output,decrypt){var Nr=w.length/4-1;var m0,m1,m2,m3,sub;if(decrypt){m0=imix[0];m1=imix[1];m2=imix[2];m3=imix[3];sub=isbox}else{m0=mix[0];m1=mix[1];m2=mix[2];m3=mix[3];sub=sbox}var a,b,c,d,a2,b2,c2;a=input[0]^w[0];b=input[decrypt?3:1]^w[1];c=input[2]^w[2];d=input[decrypt?1:3]^w[3];var i=3;for(var round=1;round<Nr;++round){a2=m0[a>>>24]^m1[b>>>16&255]^m2[c>>>8&255]^m3[d&255]^w[++i];b2=m0[b>>>24]^m1[c>>>16&255]^m2[d>>>8&255]^m3[a&255]^w[++i];c2=m0[c>>>24]^m1[d>>>16&255]^m2[a>>>8&255]^m3[b&255]^w[++i];d=m0[d>>>24]^m1[a>>>16&255]^m2[b>>>8&255]^m3[c&255]^w[++i];a=a2;b=b2;c=c2}output[0]=sub[a>>>24]<<24^sub[b>>>16&255]<<16^sub[c>>>8&255]<<8^sub[d&255]^w[++i];output[decrypt?3:1]=sub[b>>>24]<<24^sub[c>>>16&255]<<16^sub[d>>>8&255]<<8^sub[a&255]^w[++i];output[2]=sub[c>>>24]<<24^sub[d>>>16&255]<<16^sub[a>>>8&255]<<8^sub[b&255]^w[++i];output[decrypt?1:3]=sub[d>>>24]<<24^sub[a>>>16&255]<<16^sub[b>>>8&255]<<8^sub[c&255]^w[++i]}function _createCipher(options){options=options||{};var mode=(options.mode||"CBC").toUpperCase();var algorithm="AES-"+mode;var cipher;if(options.decrypt){cipher=forge.cipher.createDecipher(algorithm,options.key)}else{cipher=forge.cipher.createCipher(algorithm,options.key)}var start=cipher.start;cipher.start=function(iv,options){var output=null;if(options instanceof forge.util.ByteBuffer){output=options;options={}}options=options||{};options.output=output;options.iv=iv;start.call(cipher,options)};return cipher}},{"./cipher":11,"./cipherModes":12,"./forge":16,"./util":48}],8:[function(require,module,exports){var forge=require("./forge");require("./aes");require("./tls");var tls=module.exports=forge.tls;tls.CipherSuites["TLS_RSA_WITH_AES_128_CBC_SHA"]={id:[0,47],name:"TLS_RSA_WITH_AES_128_CBC_SHA",initSecurityParameters:function(sp){sp.bulk_cipher_algorithm=tls.BulkCipherAlgorithm.aes;sp.cipher_type=tls.CipherType.block;sp.enc_key_length=16;sp.block_length=16;sp.fixed_iv_length=16;sp.record_iv_length=16;sp.mac_algorithm=tls.MACAlgorithm.hmac_sha1;sp.mac_length=20;sp.mac_key_length=20},initConnectionState:initConnectionState};tls.CipherSuites["TLS_RSA_WITH_AES_256_CBC_SHA"]={id:[0,53],name:"TLS_RSA_WITH_AES_256_CBC_SHA",initSecurityParameters:function(sp){sp.bulk_cipher_algorithm=tls.BulkCipherAlgorithm.aes;sp.cipher_type=tls.CipherType.block;sp.enc_key_length=32;sp.block_length=16;sp.fixed_iv_length=16;sp.record_iv_length=16;sp.mac_algorithm=tls.MACAlgorithm.hmac_sha1;sp.mac_length=20;sp.mac_key_length=20},initConnectionState:initConnectionState};function initConnectionState(state,c,sp){var client=c.entity===forge.tls.ConnectionEnd.client;state.read.cipherState={init:false,cipher:forge.cipher.createDecipher("AES-CBC",client?sp.keys.server_write_key:sp.keys.client_write_key),iv:client?sp.keys.server_write_IV:sp.keys.client_write_IV};state.write.cipherState={init:false,cipher:forge.cipher.createCipher("AES-CBC",client?sp.keys.client_write_key:sp.keys.server_write_key),iv:client?sp.keys.client_write_IV:sp.keys.server_write_IV};state.read.cipherFunction=decrypt_aes_cbc_sha1;state.write.cipherFunction=encrypt_aes_cbc_sha1;state.read.macLength=state.write.macLength=sp.mac_length;state.read.macFunction=state.write.macFunction=tls.hmac_sha1}function encrypt_aes_cbc_sha1(record,s){var rval=false;var mac=s.macFunction(s.macKey,s.sequenceNumber,record);record.fragment.putBytes(mac);s.updateSequenceNumber();var iv;if(record.version.minor===tls.Versions.TLS_1_0.minor){iv=s.cipherState.init?null:s.cipherState.iv}else{iv=forge.random.getBytesSync(16)}s.cipherState.init=true;var cipher=s.cipherState.cipher;cipher.start({iv:iv});if(record.version.minor>=tls.Versions.TLS_1_1.minor){cipher.output.putBytes(iv)}cipher.update(record.fragment);if(cipher.finish(encrypt_aes_cbc_sha1_padding)){record.fragment=cipher.output;record.length=record.fragment.length();rval=true}return rval}function encrypt_aes_cbc_sha1_padding(blockSize,input,decrypt){if(!decrypt){var padding=blockSize-input.length()%blockSize;input.fillWithByte(padding-1,padding)}return true}function decrypt_aes_cbc_sha1_padding(blockSize,output,decrypt){var rval=true;if(decrypt){var len=output.length();var paddingLength=output.last();for(var i=len-1-paddingLength;i<len-1;++i){rval=rval&&output.at(i)==paddingLength}if(rval){output.truncate(paddingLength+1)}}return rval}function decrypt_aes_cbc_sha1(record,s){var rval=false;var iv;if(record.version.minor===tls.Versions.TLS_1_0.minor){iv=s.cipherState.init?null:s.cipherState.iv}else{iv=record.fragment.getBytes(16)}s.cipherState.init=true;var cipher=s.cipherState.cipher;cipher.start({iv:iv});cipher.update(record.fragment);rval=cipher.finish(decrypt_aes_cbc_sha1_padding);var macLen=s.macLength;var mac=forge.random.getBytesSync(macLen);var len=cipher.output.length();if(len>=macLen){record.fragment=cipher.output.getBytes(len-macLen);mac=cipher.output.getBytes(macLen)}else{record.fragment=cipher.output.getBytes()}record.fragment=forge.util.createBuffer(record.fragment);record.length=record.fragment.length();var mac2=s.macFunction(s.macKey,s.sequenceNumber,record);s.updateSequenceNumber();rval=compareMacs(s.macKey,mac,mac2)&&rval;return rval}function compareMacs(key,mac1,mac2){var hmac=forge.hmac.create();hmac.start("SHA1",key);hmac.update(mac1);mac1=hmac.digest().getBytes();hmac.start(null,null);hmac.update(mac2);mac2=hmac.digest().getBytes();return mac1===mac2}},{"./aes":7,"./forge":16,"./tls":47}],9:[function(require,module,exports){var forge=require("./forge");require("./util");require("./oids");var asn1=module.exports=forge.asn1=forge.asn1||{};asn1.Class={UNIVERSAL:0,APPLICATION:64,CONTEXT_SPECIFIC:128,PRIVATE:192};asn1.Type={NONE:0,BOOLEAN:1,INTEGER:2,BITSTRING:3,OCTETSTRING:4,NULL:5,OID:6,ODESC:7,EXTERNAL:8,REAL:9,ENUMERATED:10,EMBEDDED:11,UTF8:12,ROID:13,SEQUENCE:16,SET:17,PRINTABLESTRING:19,IA5STRING:22,UTCTIME:23,GENERALIZEDTIME:24,BMPSTRING:30};asn1.create=function(tagClass,type,constructed,value,options){if(forge.util.isArray(value)){var tmp=[];for(var i=0;i<value.length;++i){if(value[i]!==undefined){tmp.push(value[i])}}value=tmp}var obj={tagClass:tagClass,type:type,constructed:constructed,composed:constructed||forge.util.isArray(value),value:value};if(options&&"bitStringContents"in options){obj.bitStringContents=options.bitStringContents;obj.original=asn1.copy(obj)}return obj};asn1.copy=function(obj,options){var copy;if(forge.util.isArray(obj)){copy=[];for(var i=0;i<obj.length;++i){copy.push(asn1.copy(obj[i],options))}return copy}if(typeof obj==="string"){return obj}copy={tagClass:obj.tagClass,type:obj.type,constructed:obj.constructed,composed:obj.composed,value:asn1.copy(obj.value,options)};if(options&&!options.excludeBitStringContents){copy.bitStringContents=obj.bitStringContents}return copy};asn1.equals=function(obj1,obj2,options){if(forge.util.isArray(obj1)){if(!forge.util.isArray(obj2)){return false}if(obj1.length!==obj2.length){return false}for(var i=0;i<obj1.length;++i){if(!asn1.equals(obj1[i],obj2[i])){return false}}return true}if(typeof obj1!==typeof obj2){return false}if(typeof obj1==="string"){return obj1===obj2}var equal=obj1.tagClass===obj2.tagClass&&obj1.type===obj2.type&&obj1.constructed===obj2.constructed&&obj1.composed===obj2.composed&&asn1.equals(obj1.value,obj2.value);if(options&&options.includeBitStringContents){equal=equal&&obj1.bitStringContents===obj2.bitStringContents}return equal};asn1.getBerValueLength=function(b){var b2=b.getByte();if(b2===128){return undefined}var length;var longForm=b2&128;if(!longForm){length=b2}else{length=b.getInt((b2&127)<<3)}return length};function _checkBufferLength(bytes,remaining,n){if(n>remaining){var error=new Error("Too few bytes to parse DER.");error.available=bytes.length();error.remaining=remaining;error.requested=n;throw error}}var _getValueLength=function(bytes,remaining){var b2=bytes.getByte();remaining--;if(b2===128){return undefined}var length;var longForm=b2&128;if(!longForm){length=b2}else{var longFormBytes=b2&127;_checkBufferLength(bytes,remaining,longFormBytes);length=bytes.getInt(longFormBytes<<3)}if(length<0){throw new Error("Negative length: "+length)}return length};asn1.fromDer=function(bytes,options){if(options===undefined){options={strict:true,decodeBitStrings:true}}if(typeof options==="boolean"){options={strict:options,decodeBitStrings:true}}if(!("strict"in options)){options.strict=true}if(!("decodeBitStrings"in options)){options.decodeBitStrings=true}if(typeof bytes==="string"){bytes=forge.util.createBuffer(bytes)}return _fromDer(bytes,bytes.length(),0,options)};function _fromDer(bytes,remaining,depth,options){var start;_checkBufferLength(bytes,remaining,2);var b1=bytes.getByte();remaining--;var tagClass=b1&192;var type=b1&31;start=bytes.length();var length=_getValueLength(bytes,remaining);remaining-=start-bytes.length();if(length!==undefined&&length>remaining){if(options.strict){var error=new Error("Too few bytes to read ASN.1 value.");error.available=bytes.length();error.remaining=remaining;error.requested=length;throw error}length=remaining}var value;var bitStringContents;var constructed=(b1&32)===32;if(constructed){value=[];if(length===undefined){for(;;){_checkBufferLength(bytes,remaining,2);if(bytes.bytes(2)===String.fromCharCode(0,0)){bytes.getBytes(2);remaining-=2;break}start=bytes.length();value.push(_fromDer(bytes,remaining,depth+1,options));remaining-=start-bytes.length()}}else{while(length>0){start=bytes.length();value.push(_fromDer(bytes,length,depth+1,options));remaining-=start-bytes.length();length-=start-bytes.length()}}}if(value===undefined&&tagClass===asn1.Class.UNIVERSAL&&type===asn1.Type.BITSTRING){bitStringContents=bytes.bytes(length)}if(value===undefined&&options.decodeBitStrings&&tagClass===asn1.Class.UNIVERSAL&&type===asn1.Type.BITSTRING&&length>1){var savedRead=bytes.read;var savedRemaining=remaining;var unused=0;if(type===asn1.Type.BITSTRING){_checkBufferLength(bytes,remaining,1);unused=bytes.getByte();remaining--}if(unused===0){try{start=bytes.length();var subOptions={verbose:options.verbose,strict:true,decodeBitStrings:true};var composed=_fromDer(bytes,remaining,depth+1,subOptions);var used=start-bytes.length();remaining-=used;if(type==asn1.Type.BITSTRING){used++}var tc=composed.tagClass;if(used===length&&(tc===asn1.Class.UNIVERSAL||tc===asn1.Class.CONTEXT_SPECIFIC)){value=[composed]}}catch(ex){}}if(value===undefined){bytes.read=savedRead;remaining=savedRemaining}}if(value===undefined){if(length===undefined){if(options.strict){throw new Error("Non-constructed ASN.1 object of indefinite length.")}length=remaining}if(type===asn1.Type.BMPSTRING){value="";for(;length>0;length-=2){_checkBufferLength(bytes,remaining,2);value+=String.fromCharCode(bytes.getInt16());remaining-=2}}else{value=bytes.getBytes(length)}}var asn1Options=bitStringContents===undefined?null:{bitStringContents:bitStringContents};return asn1.create(tagClass,type,constructed,value,asn1Options)}asn1.toDer=function(obj){var bytes=forge.util.createBuffer();var b1=obj.tagClass|obj.type;var value=forge.util.createBuffer();var useBitStringContents=false;if("bitStringContents"in obj){useBitStringContents=true;if(obj.original){useBitStringContents=asn1.equals(obj,obj.original)}}if(useBitStringContents){value.putBytes(obj.bitStringContents)}else if(obj.composed){if(obj.constructed){b1|=32}else{value.putByte(0)}for(var i=0;i<obj.value.length;++i){if(obj.value[i]!==undefined){value.putBuffer(asn1.toDer(obj.value[i]))}}}else{if(obj.type===asn1.Type.BMPSTRING){for(var i=0;i<obj.value.length;++i){value.putInt16(obj.value.charCodeAt(i))}}else{if(obj.type===asn1.Type.INTEGER&&obj.value.length>1&&(obj.value.charCodeAt(0)===0&&(obj.value.charCodeAt(1)&128)===0||obj.value.charCodeAt(0)===255&&(obj.value.charCodeAt(1)&128)===128)){value.putBytes(obj.value.substr(1))}else{value.putBytes(obj.value)}}}bytes.putByte(b1);if(value.length()<=127){bytes.putByte(value.length()&127)}else{var len=value.length();var lenBytes="";do{lenBytes+=String.fromCharCode(len&255);len=len>>>8}while(len>0);bytes.putByte(lenBytes.length|128);for(var i=lenBytes.length-1;i>=0;--i){bytes.putByte(lenBytes.charCodeAt(i))}}bytes.putBuffer(value);return bytes};asn1.oidToDer=function(oid){var values=oid.split(".");var bytes=forge.util.createBuffer();bytes.putByte(40*parseInt(values[0],10)+parseInt(values[1],10));var last,valueBytes,value,b;for(var i=2;i<values.length;++i){last=true;valueBytes=[];value=parseInt(values[i],10);do{b=value&127;value=value>>>7;if(!last){b|=128}valueBytes.push(b);last=false}while(value>0);for(var n=valueBytes.length-1;n>=0;--n){bytes.putByte(valueBytes[n])}}return bytes};asn1.derToOid=function(bytes){var oid;if(typeof bytes==="string"){bytes=forge.util.createBuffer(bytes)}var b=bytes.getByte();oid=Math.floor(b/40)+"."+b%40;var value=0;while(bytes.length()>0){b=bytes.getByte();value=value<<7;if(b&128){value+=b&127}else{oid+="."+(value+b);value=0}}return oid};asn1.utcTimeToDate=function(utc){var date=new Date;var year=parseInt(utc.substr(0,2),10);year=year>=50?1900+year:2e3+year;var MM=parseInt(utc.substr(2,2),10)-1;var DD=parseInt(utc.substr(4,2),10);var hh=parseInt(utc.substr(6,2),10);var mm=parseInt(utc.substr(8,2),10);var ss=0;if(utc.length>11){var c=utc.charAt(10);var end=10;if(c!=="+"&&c!=="-"){ss=parseInt(utc.substr(10,2),10);end+=2}}date.setUTCFullYear(year,MM,DD);date.setUTCHours(hh,mm,ss,0);if(end){c=utc.charAt(end);if(c==="+"||c==="-"){var hhoffset=parseInt(utc.substr(end+1,2),10);var mmoffset=parseInt(utc.substr(end+4,2),10);var offset=hhoffset*60+mmoffset;offset*=6e4;if(c==="+"){date.setTime(+date-offset)}else{date.setTime(+date+offset)}}}return date};asn1.generalizedTimeToDate=function(gentime){var date=new Date;var YYYY=parseInt(gentime.substr(0,4),10);var MM=parseInt(gentime.substr(4,2),10)-1;var DD=parseInt(gentime.substr(6,2),10);var hh=parseInt(gentime.substr(8,2),10);var mm=parseInt(gentime.substr(10,2),10);var ss=parseInt(gentime.substr(12,2),10);var fff=0;var offset=0;var isUTC=false;if(gentime.charAt(gentime.length-1)==="Z"){isUTC=true}var end=gentime.length-5,c=gentime.charAt(end);if(c==="+"||c==="-"){var hhoffset=parseInt(gentime.substr(end+1,2),10);var mmoffset=parseInt(gentime.substr(end+4,2),10);offset=hhoffset*60+mmoffset;offset*=6e4;if(c==="+"){offset*=-1}isUTC=true}if(gentime.charAt(14)==="."){fff=parseFloat(gentime.substr(14),10)*1e3}if(isUTC){date.setUTCFullYear(YYYY,MM,DD);date.setUTCHours(hh,mm,ss,fff);date.setTime(+date+offset)}else{date.setFullYear(YYYY,MM,DD);date.setHours(hh,mm,ss,fff)}return date};asn1.dateToUtcTime=function(date){if(typeof date==="string"){return date}var rval="";var format=[];format.push((""+date.getUTCFullYear()).substr(2));format.push(""+(date.getUTCMonth()+1));format.push(""+date.getUTCDate());format.push(""+date.getUTCHours());format.push(""+date.getUTCMinutes());format.push(""+date.getUTCSeconds());for(var i=0;i<format.length;++i){if(format[i].length<2){rval+="0"}rval+=format[i]}rval+="Z";return rval};asn1.dateToGeneralizedTime=function(date){if(typeof date==="string"){return date}var rval="";var format=[];format.push(""+date.getUTCFullYear());format.push(""+(date.getUTCMonth()+1));format.push(""+date.getUTCDate());format.push(""+date.getUTCHours());format.push(""+date.getUTCMinutes());format.push(""+date.getUTCSeconds());for(var i=0;i<format.length;++i){if(format[i].length<2){rval+="0"}rval+=format[i]}rval+="Z";return rval};asn1.integerToDer=function(x){var rval=forge.util.createBuffer();if(x>=-128&&x<128){return rval.putSignedInt(x,8)}if(x>=-32768&&x<32768){return rval.putSignedInt(x,16)}if(x>=-8388608&&x<8388608){return rval.putSignedInt(x,24)}if(x>=-2147483648&&x<2147483648){return rval.putSignedInt(x,32)}var error=new Error("Integer too large; max is 32-bits.");error.integer=x;throw error};asn1.derToInteger=function(bytes){if(typeof bytes==="string"){bytes=forge.util.createBuffer(bytes)}var n=bytes.length()*8;if(n>32){throw new Error("Integer too large; max is 32-bits.")}return bytes.getSignedInt(n)};asn1.validate=function(obj,v,capture,errors){var rval=false;if((obj.tagClass===v.tagClass||typeof v.tagClass==="undefined")&&(obj.type===v.type||typeof v.type==="undefined")){if(obj.constructed===v.constructed||typeof v.constructed==="undefined"){rval=true;if(v.value&&forge.util.isArray(v.value)){var j=0;for(var i=0;rval&&i<v.value.length;++i){rval=v.value[i].optional||false;if(obj.value[j]){rval=asn1.validate(obj.value[j],v.value[i],capture,errors);if(rval){++j}else if(v.value[i].optional){rval=true}}if(!rval&&errors){errors.push("["+v.name+"] "+'Tag class "'+v.tagClass+'", type "'+v.type+'" expected value length "'+v.value.length+'", got "'+obj.value.length+'"')}}}if(rval&&capture){if(v.capture){capture[v.capture]=obj.value}if(v.captureAsn1){capture[v.captureAsn1]=obj}if(v.captureBitStringContents&&"bitStringContents"in obj){capture[v.captureBitStringContents]=obj.bitStringContents}if(v.captureBitStringValue&&"bitStringContents"in obj){var value;if(obj.bitStringContents.length<2){capture[v.captureBitStringValue]=""}else{var unused=obj.bitStringContents.charCodeAt(0);if(unused!==0){throw new Error("captureBitStringValue only supported for zero unused bits")}capture[v.captureBitStringValue]=obj.bitStringContents.slice(1)}}}}else if(errors){errors.push("["+v.name+"] "+'Expected constructed "'+v.constructed+'", got "'+obj.constructed+'"')}}else if(errors){if(obj.tagClass!==v.tagClass){errors.push("["+v.name+"] "+'Expected tag class "'+v.tagClass+'", got "'+obj.tagClass+'"')}if(obj.type!==v.type){errors.push("["+v.name+"] "+'Expected type "'+v.type+'", got "'+obj.type+'"')}}return rval};var _nonLatinRegex=/[^\\u0000-\\u00ff]/;asn1.prettyPrint=function(obj,level,indentation){var rval="";level=level||0;indentation=indentation||2;if(level>0){rval+="\n"}var indent="";for(var i=0;i<level*indentation;++i){indent+=" "}rval+=indent+"Tag: ";switch(obj.tagClass){case asn1.Class.UNIVERSAL:rval+="Universal:";break;case asn1.Class.APPLICATION:rval+="Application:";break;case asn1.Class.CONTEXT_SPECIFIC:rval+="Context-Specific:";break;case asn1.Class.PRIVATE:rval+="Private:";break}if(obj.tagClass===asn1.Class.UNIVERSAL){rval+=obj.type;switch(obj.type){case asn1.Type.NONE:rval+=" (None)";break;case asn1.Type.BOOLEAN:rval+=" (Boolean)";break;case asn1.Type.INTEGER:rval+=" (Integer)";break;case asn1.Type.BITSTRING:rval+=" (Bit string)";break;case asn1.Type.OCTETSTRING:rval+=" (Octet string)";break;case asn1.Type.NULL:rval+=" (Null)";break;case asn1.Type.OID:rval+=" (Object Identifier)";break;case asn1.Type.ODESC:rval+=" (Object Descriptor)";break;case asn1.Type.EXTERNAL:rval+=" (External or Instance of)";break;case asn1.Type.REAL:rval+=" (Real)";break;case asn1.Type.ENUMERATED:rval+=" (Enumerated)";break;case asn1.Type.EMBEDDED:rval+=" (Embedded PDV)";break;case asn1.Type.UTF8:rval+=" (UTF8)";break;case asn1.Type.ROID:rval+=" (Relative Object Identifier)";break;case asn1.Type.SEQUENCE:rval+=" (Sequence)";break;case asn1.Type.SET:rval+=" (Set)";break;case asn1.Type.PRINTABLESTRING:rval+=" (Printable String)";break;case asn1.Type.IA5String:rval+=" (IA5String (ASCII))";break;case asn1.Type.UTCTIME:rval+=" (UTC time)";break;case asn1.Type.GENERALIZEDTIME:rval+=" (Generalized time)";break;case asn1.Type.BMPSTRING:rval+=" (BMP String)";break}}else{rval+=obj.type}rval+="\n";rval+=indent+"Constructed: "+obj.constructed+"\n";if(obj.composed){var subvalues=0;var sub="";for(var i=0;i<obj.value.length;++i){if(obj.value[i]!==undefined){subvalues+=1;sub+=asn1.prettyPrint(obj.value[i],level+1,indentation);if(i+1<obj.value.length){sub+=","}}}rval+=indent+"Sub values: "+subvalues+sub}else{rval+=indent+"Value: ";if(obj.type===asn1.Type.OID){var oid=asn1.derToOid(obj.value);rval+=oid;if(forge.pki&&forge.pki.oids){if(oid in forge.pki.oids){rval+=" ("+forge.pki.oids[oid]+") "}}}if(obj.type===asn1.Type.INTEGER){try{rval+=asn1.derToInteger(obj.value)}catch(ex){rval+="0x"+forge.util.bytesToHex(obj.value)}}else if(obj.type===asn1.Type.BITSTRING){if(obj.value.length>1){rval+="0x"+forge.util.bytesToHex(obj.value.slice(1))}else{rval+="(none)"}if(obj.value.length>0){var unused=obj.value.charCodeAt(0);if(unused==1){rval+=" (1 unused bit shown)"}else if(unused>1){rval+=" ("+unused+" unused bits shown)"}}}else if(obj.type===asn1.Type.OCTETSTRING){if(!_nonLatinRegex.test(obj.value)){rval+="("+obj.value+") "}rval+="0x"+forge.util.bytesToHex(obj.value)}else if(obj.type===asn1.Type.UTF8){rval+=forge.util.decodeUtf8(obj.value)}else if(obj.type===asn1.Type.PRINTABLESTRING||obj.type===asn1.Type.IA5String){rval+=obj.value}else if(_nonLatinRegex.test(obj.value)){rval+="0x"+forge.util.bytesToHex(obj.value)}else if(obj.value.length===0){rval+="[null]"}else{rval+=obj.value}}return rval}},{"./forge":16,"./oids":27,"./util":48}],10:[function(require,module,exports){(function(Buffer){var api={};module.exports=api;var _reverseAlphabets={};api.encode=function(input,alphabet,maxline){if(typeof alphabet!=="string"){throw new TypeError('"alphabet" must be a string.')}if(maxline!==undefined&&typeof maxline!=="number"){throw new TypeError('"maxline" must be a number.')}var output="";if(!(input instanceof Uint8Array)){output=_encodeWithByteBuffer(input,alphabet)}else{var i=0;var base=alphabet.length;var first=alphabet.charAt(0);var digits=[0];for(i=0;i<input.length;++i){for(var j=0,carry=input[i];j<digits.length;++j){carry+=digits[j]<<8;digits[j]=carry%base;carry=carry/base|0}while(carry>0){digits.push(carry%base);carry=carry/base|0}}for(i=0;input[i]===0&&i<input.length-1;++i){output+=first}for(i=digits.length-1;i>=0;--i){output+=alphabet[digits[i]]}}if(maxline){var regex=new RegExp(".{1,"+maxline+"}","g");output=output.match(regex).join("\r\n")}return output};api.decode=function(input,alphabet){if(typeof input!=="string"){throw new TypeError('"input" must be a string.')}if(typeof alphabet!=="string"){throw new TypeError('"alphabet" must be a string.')}var table=_reverseAlphabets[alphabet];if(!table){table=_reverseAlphabets[alphabet]=[];for(var i=0;i<alphabet.length;++i){table[alphabet.charCodeAt(i)]=i}}input=input.replace(/\s/g,"");var base=alphabet.length;var first=alphabet.charAt(0);var bytes=[0];for(var i=0;i<input.length;i++){var value=table[input.charCodeAt(i)];if(value===undefined){return}for(var j=0,carry=value;j<bytes.length;++j){carry+=bytes[j]*base;bytes[j]=carry&255;carry>>=8}while(carry>0){bytes.push(carry&255);carry>>=8}}for(var k=0;input[k]===first&&k<input.length-1;++k){bytes.push(0)}if(typeof Buffer!=="undefined"){return Buffer.from(bytes.reverse())}return new Uint8Array(bytes.reverse())};function _encodeWithByteBuffer(input,alphabet){var i=0;var base=alphabet.length;var first=alphabet.charAt(0);var digits=[0];for(i=0;i<input.length();++i){for(var j=0,carry=input.at(i);j<digits.length;++j){carry+=digits[j]<<8;digits[j]=carry%base;carry=carry/base|0}while(carry>0){digits.push(carry%base);carry=carry/base|0}}var output="";for(i=0;input.at(i)===0&&i<input.length()-1;++i){output+=first}for(i=digits.length-1;i>=0;--i){output+=alphabet[digits[i]]}return output}}).call(this,require("buffer").Buffer)},{buffer:6}],11:[function(require,module,exports){var forge=require("./forge");require("./util");module.exports=forge.cipher=forge.cipher||{};forge.cipher.algorithms=forge.cipher.algorithms||{};forge.cipher.createCipher=function(algorithm,key){var api=algorithm;if(typeof api==="string"){api=forge.cipher.getAlgorithm(api);if(api){api=api()}}if(!api){throw new Error("Unsupported algorithm: "+algorithm)}return new forge.cipher.BlockCipher({algorithm:api,key:key,decrypt:false})};forge.cipher.createDecipher=function(algorithm,key){var api=algorithm;if(typeof api==="string"){api=forge.cipher.getAlgorithm(api);if(api){api=api()}}if(!api){throw new Error("Unsupported algorithm: "+algorithm)}return new forge.cipher.BlockCipher({algorithm:api,key:key,decrypt:true})};forge.cipher.registerAlgorithm=function(name,algorithm){name=name.toUpperCase();forge.cipher.algorithms[name]=algorithm};forge.cipher.getAlgorithm=function(name){name=name.toUpperCase();if(name in forge.cipher.algorithms){return forge.cipher.algorithms[name]}return null};var BlockCipher=forge.cipher.BlockCipher=function(options){this.algorithm=options.algorithm;this.mode=this.algorithm.mode;this.blockSize=this.mode.blockSize;this._finish=false;this._input=null;this.output=null;this._op=options.decrypt?this.mode.decrypt:this.mode.encrypt;this._decrypt=options.decrypt;this.algorithm.initialize(options)};BlockCipher.prototype.start=function(options){options=options||{};var opts={};for(var key in options){opts[key]=options[key]}opts.decrypt=this._decrypt;this._finish=false;this._input=forge.util.createBuffer();this.output=options.output||forge.util.createBuffer();this.mode.start(opts)};BlockCipher.prototype.update=function(input){if(input){this._input.putBuffer(input)}while(!this._op.call(this.mode,this._input,this.output,this._finish)&&!this._finish){}this._input.compact()};BlockCipher.prototype.finish=function(pad){if(pad&&(this.mode.name==="ECB"||this.mode.name==="CBC")){this.mode.pad=function(input){return pad(this.blockSize,input,false)};this.mode.unpad=function(output){return pad(this.blockSize,output,true)}}var options={};options.decrypt=this._decrypt;options.overflow=this._input.length()%this.blockSize;if(!this._decrypt&&this.mode.pad){if(!this.mode.pad(this._input,options)){return false}}this._finish=true;this.update();if(this._decrypt&&this.mode.unpad){if(!this.mode.unpad(this.output,options)){return false}}if(this.mode.afterFinish){if(!this.mode.afterFinish(this.output,options)){return false}}return true}},{"./forge":16,"./util":48}],12:[function(require,module,exports){var forge=require("./forge");require("./util");forge.cipher=forge.cipher||{};var modes=module.exports=forge.cipher.modes=forge.cipher.modes||{};modes.ecb=function(options){options=options||{};this.name="ECB";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=new Array(this._ints);this._outBlock=new Array(this._ints)};modes.ecb.prototype.start=function(options){};modes.ecb.prototype.encrypt=function(input,output,finish){if(input.length()<this.blockSize&&!(finish&&input.length()>0)){return true}for(var i=0;i<this._ints;++i){this._inBlock[i]=input.getInt32()}this.cipher.encrypt(this._inBlock,this._outBlock);for(var i=0;i<this._ints;++i){output.putInt32(this._outBlock[i])}};modes.ecb.prototype.decrypt=function(input,output,finish){if(input.length()<this.blockSize&&!(finish&&input.length()>0)){return true}for(var i=0;i<this._ints;++i){this._inBlock[i]=input.getInt32()}this.cipher.decrypt(this._inBlock,this._outBlock);for(var i=0;i<this._ints;++i){output.putInt32(this._outBlock[i])}};modes.ecb.prototype.pad=function(input,options){var padding=input.length()===this.blockSize?this.blockSize:this.blockSize-input.length();input.fillWithByte(padding,padding);return true};modes.ecb.prototype.unpad=function(output,options){if(options.overflow>0){return false}var len=output.length();var count=output.at(len-1);if(count>this.blockSize<<2){return false}output.truncate(count);return true};modes.cbc=function(options){options=options||{};this.name="CBC";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=new Array(this._ints);this._outBlock=new Array(this._ints)};modes.cbc.prototype.start=function(options){if(options.iv===null){if(!this._prev){throw new Error("Invalid IV parameter.")}this._iv=this._prev.slice(0)}else if(!("iv"in options)){throw new Error("Invalid IV parameter.")}else{this._iv=transformIV(options.iv);this._prev=this._iv.slice(0)}};modes.cbc.prototype.encrypt=function(input,output,finish){if(input.length()<this.blockSize&&!(finish&&input.length()>0)){return true}for(var i=0;i<this._ints;++i){this._inBlock[i]=this._prev[i]^input.getInt32()}this.cipher.encrypt(this._inBlock,this._outBlock);for(var i=0;i<this._ints;++i){output.putInt32(this._outBlock[i])}this._prev=this._outBlock};modes.cbc.prototype.decrypt=function(input,output,finish){if(input.length()<this.blockSize&&!(finish&&input.length()>0)){return true}for(var i=0;i<this._ints;++i){this._inBlock[i]=input.getInt32()}this.cipher.decrypt(this._inBlock,this._outBlock);for(var i=0;i<this._ints;++i){output.putInt32(this._prev[i]^this._outBlock[i])}this._prev=this._inBlock.slice(0)};modes.cbc.prototype.pad=function(input,options){var padding=input.length()===this.blockSize?this.blockSize:this.blockSize-input.length();input.fillWithByte(padding,padding);return true};modes.cbc.prototype.unpad=function(output,options){if(options.overflow>0){return false}var len=output.length();var count=output.at(len-1);if(count>this.blockSize<<2){return false}output.truncate(count);return true};modes.cfb=function(options){options=options||{};this.name="CFB";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=null;this._outBlock=new Array(this._ints);this._partialBlock=new Array(this._ints);this._partialOutput=forge.util.createBuffer();this._partialBytes=0};modes.cfb.prototype.start=function(options){if(!("iv"in options)){throw new Error("Invalid IV parameter.")}this._iv=transformIV(options.iv);this._inBlock=this._iv.slice(0);this._partialBytes=0};modes.cfb.prototype.encrypt=function(input,output,finish){var inputLength=input.length();if(inputLength===0){return true}this.cipher.encrypt(this._inBlock,this._outBlock);if(this._partialBytes===0&&inputLength>=this.blockSize){for(var i=0;i<this._ints;++i){this._inBlock[i]=input.getInt32()^this._outBlock[i];output.putInt32(this._inBlock[i])}return}var partialBytes=(this.blockSize-inputLength)%this.blockSize;if(partialBytes>0){partialBytes=this.blockSize-partialBytes}this._partialOutput.clear();for(var i=0;i<this._ints;++i){this._partialBlock[i]=input.getInt32()^this._outBlock[i];this._partialOutput.putInt32(this._partialBlock[i])}if(partialBytes>0){input.read-=this.blockSize}else{for(var i=0;i<this._ints;++i){this._inBlock[i]=this._partialBlock[i]}}if(this._partialBytes>0){this._partialOutput.getBytes(this._partialBytes)}if(partialBytes>0&&!finish){output.putBytes(this._partialOutput.getBytes(partialBytes-this._partialBytes));this._partialBytes=partialBytes;return true}output.putBytes(this._partialOutput.getBytes(inputLength-this._partialBytes));this._partialBytes=0};modes.cfb.prototype.decrypt=function(input,output,finish){var inputLength=input.length();if(inputLength===0){return true}this.cipher.encrypt(this._inBlock,this._outBlock);if(this._partialBytes===0&&inputLength>=this.blockSize){for(var i=0;i<this._ints;++i){this._inBlock[i]=input.getInt32();output.putInt32(this._inBlock[i]^this._outBlock[i])}return}var partialBytes=(this.blockSize-inputLength)%this.blockSize;if(partialBytes>0){partialBytes=this.blockSize-partialBytes}this._partialOutput.clear();for(var i=0;i<this._ints;++i){this._partialBlock[i]=input.getInt32();this._partialOutput.putInt32(this._partialBlock[i]^this._outBlock[i])}if(partialBytes>0){input.read-=this.blockSize}else{for(var i=0;i<this._ints;++i){this._inBlock[i]=this._partialBlock[i]}}if(this._partialBytes>0){this._partialOutput.getBytes(this._partialBytes)}if(partialBytes>0&&!finish){output.putBytes(this._partialOutput.getBytes(partialBytes-this._partialBytes));this._partialBytes=partialBytes;return true}output.putBytes(this._partialOutput.getBytes(inputLength-this._partialBytes));this._partialBytes=0};modes.ofb=function(options){options=options||{};this.name="OFB";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=null;this._outBlock=new Array(this._ints);this._partialOutput=forge.util.createBuffer();this._partialBytes=0};modes.ofb.prototype.start=function(options){if(!("iv"in options)){throw new Error("Invalid IV parameter.")}this._iv=transformIV(options.iv);this._inBlock=this._iv.slice(0);this._partialBytes=0};modes.ofb.prototype.encrypt=function(input,output,finish){var inputLength=input.length();if(input.length()===0){return true}this.cipher.encrypt(this._inBlock,this._outBlock);if(this._partialBytes===0&&inputLength>=this.blockSize){for(var i=0;i<this._ints;++i){output.putInt32(input.getInt32()^this._outBlock[i]);this._inBlock[i]=this._outBlock[i]}return}var partialBytes=(this.blockSize-inputLength)%this.blockSize;if(partialBytes>0){partialBytes=this.blockSize-partialBytes}this._partialOutput.clear();for(var i=0;i<this._ints;++i){this._partialOutput.putInt32(input.getInt32()^this._outBlock[i])}if(partialBytes>0){input.read-=this.blockSize}else{for(var i=0;i<this._ints;++i){this._inBlock[i]=this._outBlock[i]}}if(this._partialBytes>0){this._partialOutput.getBytes(this._partialBytes)}if(partialBytes>0&&!finish){output.putBytes(this._partialOutput.getBytes(partialBytes-this._partialBytes));this._partialBytes=partialBytes;return true}output.putBytes(this._partialOutput.getBytes(inputLength-this._partialBytes));this._partialBytes=0};modes.ofb.prototype.decrypt=modes.ofb.prototype.encrypt;modes.ctr=function(options){options=options||{};this.name="CTR";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=null;this._outBlock=new Array(this._ints);this._partialOutput=forge.util.createBuffer();this._partialBytes=0};modes.ctr.prototype.start=function(options){if(!("iv"in options)){throw new Error("Invalid IV parameter.")}this._iv=transformIV(options.iv);this._inBlock=this._iv.slice(0);this._partialBytes=0};modes.ctr.prototype.encrypt=function(input,output,finish){var inputLength=input.length();if(inputLength===0){return true}this.cipher.encrypt(this._inBlock,this._outBlock);if(this._partialBytes===0&&inputLength>=this.blockSize){for(var i=0;i<this._ints;++i){output.putInt32(input.getInt32()^this._outBlock[i])}}else{var partialBytes=(this.blockSize-inputLength)%this.blockSize;if(partialBytes>0){partialBytes=this.blockSize-partialBytes}this._partialOutput.clear();for(var i=0;i<this._ints;++i){this._partialOutput.putInt32(input.getInt32()^this._outBlock[i])}if(partialBytes>0){input.read-=this.blockSize}if(this._partialBytes>0){this._partialOutput.getBytes(this._partialBytes)}if(partialBytes>0&&!finish){output.putBytes(this._partialOutput.getBytes(partialBytes-this._partialBytes));this._partialBytes=partialBytes;return true}output.putBytes(this._partialOutput.getBytes(inputLength-this._partialBytes));this._partialBytes=0}inc32(this._inBlock)};modes.ctr.prototype.decrypt=modes.ctr.prototype.encrypt;modes.gcm=function(options){options=options||{};this.name="GCM";this.cipher=options.cipher;this.blockSize=options.blockSize||16;this._ints=this.blockSize/4;this._inBlock=new Array(this._ints);this._outBlock=new Array(this._ints);this._partialOutput=forge.util.createBuffer();this._partialBytes=0;this._R=3774873600};modes.gcm.prototype.start=function(options){if(!("iv"in options)){throw new Error("Invalid IV parameter.")}var iv=forge.util.createBuffer(options.iv);this._cipherLength=0;var additionalData;if("additionalData"in options){additionalData=forge.util.createBuffer(options.additionalData)}else{additionalData=forge.util.createBuffer()}if("tagLength"in options){this._tagLength=options.tagLength}else{this._tagLength=128}this._tag=null;if(options.decrypt){this._tag=forge.util.createBuffer(options.tag).getBytes();if(this._tag.length!==this._tagLength/8){throw new Error("Authentication tag does not match tag length.")}}this._hashBlock=new Array(this._ints);this.tag=null;this._hashSubkey=new Array(this._ints);this.cipher.encrypt([0,0,0,0],this._