rsa.js 9.5 KB

1
  1. var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.arrayIteratorImpl=function(b){var d=0;return function(){return d<b.length?{done:!1,value:b[d++]}:{done:!0}}};$jscomp.arrayIterator=function(b){return{next:$jscomp.arrayIteratorImpl(b)}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(b,d,f){b!=Array.prototype&&b!=Object.prototype&&(b[d]=f.value)};$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global&&null!=global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.SymbolClass=function(b,d){this.$jscomp$symbol$id_=b;$jscomp.defineProperty(this,"description",{configurable:!0,writable:!0,value:d})};$jscomp.SymbolClass.prototype.toString=function(){return this.$jscomp$symbol$id_};$jscomp.Symbol=function(){function b(f){if(this instanceof b)throw new TypeError("Symbol is not a constructor");return new $jscomp.SymbolClass($jscomp.SYMBOL_PREFIX+(f||"")+"_"+d++,f)}var d=0;return b}();$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var b=$jscomp.global.Symbol.iterator;b||(b=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("Symbol.iterator"));"function"!=typeof Array.prototype[b]&&$jscomp.defineProperty(Array.prototype,b,{configurable:!0,writable:!0,value:function(){return $jscomp.iteratorPrototype($jscomp.arrayIteratorImpl(this))}});$jscomp.initSymbolIterator=function(){}};$jscomp.initSymbolAsyncIterator=function(){$jscomp.initSymbol();var b=$jscomp.global.Symbol.asyncIterator;b||(b=$jscomp.global.Symbol.asyncIterator=$jscomp.global.Symbol("Symbol.asyncIterator"));$jscomp.initSymbolAsyncIterator=function(){}};$jscomp.iteratorPrototype=function(b){$jscomp.initSymbolIterator();b={next:b};b[$jscomp.global.Symbol.iterator]=function(){return this};return b};$jscomp.iteratorFromArray=function(b,d){$jscomp.initSymbolIterator();b instanceof String&&(b+="");var f=0,h={next:function(){if(f<b.length){var c=f++;return{value:d(c,b[c]),done:!1}}h.next=function(){return{done:!0,value:void 0}};return h.next()}};h[Symbol.iterator]=function(){return h};return h};$jscomp.polyfill=function(b,d,f,h){if(d){f=$jscomp.global;b=b.split(".");for(h=0;h<b.length-1;h++){var c=b[h];c in f||(f[c]={});f=f[c]}b=b[b.length-1];h=f[b];d=d(h);d!=h&&null!=d&&$jscomp.defineProperty(f,b,{configurable:!0,writable:!0,value:d})}};$jscomp.polyfill("Array.prototype.keys",function(b){return b?b:function(){return $jscomp.iteratorFromArray(this,function(b){return b})}},"es6","es3");(function(){function b(b){"undefined"===typeof BigInteger&&(BigInteger=b.jsbn.BigInteger);var c=b.asn1,k=b.jsustoolkitErrCode=b.jsustoolkitErrCode||{};b.pki=b.pki||{};b.pki.rsa=b.rsa=b.rsa||{};var d=b.pki,f=function(a){if(a.algorithm in b.pki.oids)var l=b.pki.oids[a.algorithm];else throw{code:"101001",message:k["101001"]+"("+a.algorithm+")"};var e=c.oidToDer(l).getBytes();l=c.create(c.Class.UNIVERSAL,c.Type.SEQUENCE,!0,[]);var g=c.create(c.Class.UNIVERSAL,c.Type.SEQUENCE,!0,[]);g.value.push(c.create(c.Class.UNIVERSAL,c.Type.OID,!1,e));g.value.push(c.create(c.Class.UNIVERSAL,c.Type.NULL,!1,""));a=c.create(c.Class.UNIVERSAL,c.Type.OCTETSTRING,!1,a.digest().getBytes());l.value.push(g);l.value.push(a);return c.toDer(l).getBytes()},h=function(a,b,e){if(e)b=a.modPow(b.e,b.n);else{b.dP||(b.dP=b.d.mod(b.p.subtract(BigInteger.ONE)));b.dQ||(b.dQ=b.d.mod(b.q.subtract(BigInteger.ONE)));b.qInv||(b.qInv=b.q.modInverse(b.p));e=a.mod(b.p).modPow(b.dP,b.p);for(a=a.mod(b.q).modPow(b.dQ,b.q);0>e.compareTo(a);)e=e.add(b.p);b=e.subtract(a).multiply(b.qInv).mod(b.p).multiply(b.q).add(a)}return b};d.rsa.encrypt=function(a,c,e){if(null==a||"undefined"==typeof a)throw{code:"101002",message:k["101002"]};if(null==c||"undefined"==typeof c)throw{code:"101003",message:k["101003"]};if(null==e||"undefined"==typeof e)throw{code:"101004",message:k["101004"]};var g=e,l=b.util.createBuffer(),d=Math.ceil(c.n.bitLength()/8);if(!1!==e&&!0!==e){if(a.length>d-11)throw{code:"101005",message:k["101005"]+"(length:"+a.length+", max:"+(d-11)+")"};l.putByte(0);l.putByte(e);var f=d-3-a.length;if(0===e||1===e){g=!1;e=0===e?0:255;for(var m=0;m<f;++m)l.putByte(e)}else for(g=!0,m=0;m<f;++m)e=Math.floor(255*Math.random())+1,l.putByte(e);l.putByte(0)}l.putBytes(a);a=new BigInteger(l.toHex(),16);c=h(a,c,g).toString(16);g=b.util.createBuffer();for(d-=Math.ceil(c.length/2);0<d;)g.putByte(0),--d;g.putBytes(b.util.hexToBytes(c));return g.getBytes()};d.rsa.decrypt=function(a,c,e,d){if(null==a||"undefined"==typeof a)throw{code:"101006",message:k["101006"]};if(null==c||"undefined"==typeof c)throw{code:"101007",message:k["101007"]};if(null==e||"undefined"==typeof e)throw{code:"101008",message:k["101008"]};var g=Math.ceil(c.n.bitLength()/8);if(a.length!=g)throw{code:"101009",message:k["101009"]+"(length: "+a.length+", expected: "+g+")"};a=new BigInteger(b.util.createBuffer(a).toHex(),16);a=h(a,c,e).toString(16);c=b.util.createBuffer();for(var f=g-Math.ceil(a.length/2);0<f;)c.putByte(0),--f;c.putBytes(b.util.hexToBytes(a));if(!1!==d){f=c.getByte();a=c.getByte();if(0!==f||e&&0!==a&&1!==a||!e&&2!=a||e&&0===a&&"undefined"===typeof d)throw{code:"101010",message:k["101010"]};e=0;if(0===a)for(e=g-3-d,d=0;d<e;++d){if(0!==c.getByte())throw{code:"101011",message:k["101011"]};}else if(1===a)for(e=0;1<c.length();){if(255!==c.getByte()){--c.read;break}++e}else if(2===a)for(e=0;1<c.length();){if(0===c.getByte()){--c.read;break}++e}if(0!==c.getByte()||e!==g-3-c.length())throw{code:"101013",message:k["101013"]};}return c.getBytes()};d.rsa.createKeyPairGenerationState=function(a,c){"string"===typeof a&&(a=parseInt(a,10));a=a||1024;a={state:0,itrs:0,maxItrs:100,bits:a,rng:{nextBytes:function(a){for(var c=b.random.getBytes(a.length),d=0;d<a.length;++d)a[d]=c.charCodeAt(d)}},e:new BigInteger((c||65537).toString(16),16),p:null,q:null,qBits:a>>1,pBits:a-(a>>1),pqState:0,num:null,six:new BigInteger(null),addNext:2,keys:null};a.six.fromInt(6);return a};d.rsa.stepKeyPairGenerationState=function(a,c){for(var d=+new Date,g,f=0;null===a.keys&&(0>=c||f<c);){if(0===a.state){g=null===a.p?a.pBits:a.qBits;var k=g-1;if(0===a.pqState)a.itrs=0,a.num=new BigInteger(g,a.rng),a.r=null,a.num.isEven()&&a.num.dAddOffset(1,0),a.num.testBit(k)||a.num.bitwiseTo(BigInteger.ONE.shiftLeft(k),function(a,b){return a|b},a.num),++a.pqState;else if(1===a.pqState){if(null===a.addNext){var h=a.num.mod(a.six).byteValue();3===h&&(a.num.mod.dAddOffset(2),h=5);a.addNext=1===h?2:4}a.num.isProbablePrime(1)?++a.pqState:a.itrs<a.maxItrs?(a.num.dAddOffset(a.addNext,0),a.num.bitLength()>g?(a.addNext=null,a.num.subTo(BigInteger.ONE.shiftLeft(k),a.num)):a.addNext=4===a.addNext?2:4,++a.itrs):a.pqState=0}else 2===a.pqState?a.pqState=0===a.num.subtract(BigInteger.ONE).gcd(a.e).compareTo(BigInteger.ONE)?3:0:3===a.pqState&&(a.pqState=0,a.num.isProbablePrime(10)&&(null===a.p?a.p=a.num:a.q=a.num,null!==a.p&&null!==a.q&&++a.state),a.num=null)}else 1===a.state?(0>a.p.compareTo(a.q)&&(a.num=a.p,a.p=a.q,a.q=a.num),++a.state):2===a.state?(a.p1=a.p.subtract(BigInteger.ONE),a.q1=a.q.subtract(BigInteger.ONE),a.phi=a.p1.multiply(a.q1),++a.state):3===a.state?0===a.phi.gcd(a.e).compareTo(BigInteger.ONE)?++a.state:(a.p=null,a.q=null,a.state=0):4===a.state?(a.n=a.p.multiply(a.q),a.n.bitLength()===a.bits?++a.state:(a.q=null,a.state=0)):5===a.state&&(g=a.e.modInverse(a.phi),a.keys={privateKey:b.pki.rsa.setPrivateKey(a.n,a.e,g,a.p,a.q,g.mod(a.p1),g.mod(a.q1),a.q.modInverse(a.p)),publicKey:b.pki.rsa.setPublicKey(a.n,a.e)});g=+new Date;f+=g-d;d=g}return null!==a.keys};d.rsa.generateKeyPair=function(a,b){a=d.rsa.createKeyPairGenerationState(a,b);d.rsa.stepKeyPairGenerationState(a,0);return a.keys};d.rsa.setPublicKey=function(a,b){var e={n:a,e:b,encrypt:function(a){return d.rsa.encrypt(a,e,2)},verify:function(a,b,f){if(null==a||"undefined"==typeof a)throw{code:"104002",message:k["104002"]};if(null==b||"undefined"==typeof b)throw{code:"104003",message:k["104003"]};b=d.rsa.decrypt(b,e,!0,void 0===f?void 0:!1);return void 0===f?(f=c.fromDer(b),a===f.value[1].value):f.verify(a,b,e.n.bitLength())}};return e};d.rsa.setPrivateKey=function(a,h,e,g,p,n,q,m){var l={n:a,e:h,d:e,p:g,q:p,dP:n,dQ:q,qInv:m,decrypt:function(a){return d.rsa.decrypt(a,l,!1)},sign:function(a,b){if(null==a||"undefined"==typeof a)throw{code:"104001",message:k["104001"]};var c=!1;void 0===b&&(b={encode:f},c=1);a=b.encode(a,l.n.bitLength());return d.rsa.encrypt(a,l,c)},signWithHash:function(a,e){if(null==a||"undefined"==typeof a)throw{code:"104002",message:k["104002"]};var f=!1;void 0===e&&(f=1);var g=c.oidToDer(b.pki.oids.sha256).getBytes();e=c.create(c.Class.UNIVERSAL,c.Type.SEQUENCE,!0,[]);var h=c.create(c.Class.UNIVERSAL,c.Type.SEQUENCE,!0,[]);h.value.push(c.create(c.Class.UNIVERSAL,c.Type.OID,!1,g));h.value.push(c.create(c.Class.UNIVERSAL,c.Type.NULL,!1,""));a=c.create(c.Class.UNIVERSAL,c.Type.OCTETSTRING,!1,a);e.value.push(h);e.value.push(a);return d.rsa.encrypt(c.toDer(e).getBytes(),l,f)}};return l}}var d="./asn1 ./oids ./random ./util ./jsbn ./jsustoolkitErrCode".split(" "),f=null;"function"!==typeof define&&("object"===typeof module&&module.exports?f=function(b,c){c(require,module)}:(crosscert=window.crosscert=window.crosscert||{},b(crosscert)));(f||"function"===typeof define)&&(f||define)(["require","module"].concat(d),function(f,c){c.exports=function(c){var h=d.map(function(b){return f(b)}).concat(b);c=c||{};c.defined=c.defined||{};if(c.defined.rsa)return c.rsa;c.defined.rsa=!0;for(var k=0;k<h.length;++k)h[k](c);return c.rsa}})})();