forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsasign-1.2.min.js
3 lines (3 loc) · 5.76 KB
/
rsasign-1.2.min.js
1
2
3
/*! rsasign-1.2.js (c) 2012 Kenji Urushima | kjur.github.com/jsrsasign/license
*/
var _RSASIGN_DIHEAD=[];_RSASIGN_DIHEAD.sha1="3021300906052b0e03021a05000414";_RSASIGN_DIHEAD.sha256="3031300d060960864801650304020105000420";_RSASIGN_DIHEAD.sha384="3041300d060960864801650304020205000430";_RSASIGN_DIHEAD.sha512="3051300d060960864801650304020305000440";_RSASIGN_DIHEAD.md2="3020300c06082a864886f70d020205000410";_RSASIGN_DIHEAD.md5="3020300c06082a864886f70d020505000410";_RSASIGN_DIHEAD.ripemd160="3021300906052b2403020105000414";var _RSASIGN_HASHHEXFUNC=[];_RSASIGN_HASHHEXFUNC.sha1=function(a){return hex_sha1(a)};_RSASIGN_HASHHEXFUNC.sha256=function(a){return hex_sha256(a)};_RSASIGN_HASHHEXFUNC.sha512=function(a){return hex_sha512(a)};_RSASIGN_HASHHEXFUNC.md5=function(a){return hex_md5(a)};_RSASIGN_HASHHEXFUNC.ripemd160=function(a){return hex_rmd160(a)};var _RE_HEXDECONLY=new RegExp("");_RE_HEXDECONLY.compile("[^0-9a-f]","gi");function _rsasign_getHexPaddedDigestInfoForString(l,f,b){var c=f/4;var h=_RSASIGN_HASHHEXFUNC[b];var d=h(l);var a="0001";var j="00"+_RSASIGN_DIHEAD[b]+d;var g="";var k=c-a.length-j.length;for(var e=0;e<k;e+=2){g+="ff"}sPaddedMessageHex=a+g+j;return sPaddedMessageHex}function _zeroPaddingOfSignature(e,d){var c="";var a=d/4-e.length;for(var b=0;b<a;b++){c=c+"0"}return c+e}function _rsasign_signString(e,c){var f=_rsasign_getHexPaddedDigestInfoForString(e,this.n.bitLength(),c);var b=parseBigInt(f,16);var d=this.doPrivate(b);var a=d.toString(16);return _zeroPaddingOfSignature(a,this.n.bitLength())}function _rsasign_signStringWithSHA1(a){return _rsasign_signString.call(this,a,"sha1")}function _rsasign_signStringWithSHA256(a){return _rsasign_signString.call(this,a,"sha256")}function pss_mgf1_str(c,a,e){var b="",d=0;while(b.length<a){b+=e(c+String.fromCharCode.apply(String,[(d&4278190080)>>24,(d&16711680)>>16,(d&65280)>>8,d&255]));d+=1}return b}function _rsasign_signStringPSS(q,a,k){var n=_RSASIGN_HASHRAWFUNC[a];var b=n(q);var g=b.length;var l=this.n.bitLength()-1;var c=Math.ceil(l/8);var d;if(k===-1){k=g}else{if((k===-2)||(k===undefined)){k=c-g-2}else{if(k<-2){throw"invalid salt length"}}}if(c<(g+k+2)){throw"data too long"}var f="";if(k>0){f=new Array(k);new SecureRandom().nextBytes(f);f=String.fromCharCode.apply(String,f)}var m=n("\x00\x00\x00\x00\x00\x00\x00\x00"+b+f);var j=[];for(d=0;d<c-k-g-2;d+=1){j[d]=0}var e=String.fromCharCode.apply(String,j)+"\x01"+f;var h=pss_mgf1_str(m,e.length,n);var p=[];for(d=0;d<e.length;d+=1){p[d]=e.charCodeAt(d)^h.charCodeAt(d)}var o=(65280>>(8*c-l))&255;p[0]&=~o;for(d=0;d<g;d++){p.push(m.charCodeAt(d))}p.push(188);return _zeroPaddingOfSignature(this.doPrivate(new BigInteger(p)).toString(16),this.n.bitLength())}function _rsasign_getDecryptSignatureBI(a,d,c){var b=new RSAKey();b.setPublic(d,c);var e=b.doPublic(a);return e}function _rsasign_getHexDigestInfoFromSig(a,c,b){var e=_rsasign_getDecryptSignatureBI(a,c,b);var d=e.toString(16).replace(/^1f+00/,"");return d}function _rsasign_getAlgNameAndHashFromHexDisgestInfo(f){for(var e in _RSASIGN_DIHEAD){var d=_RSASIGN_DIHEAD[e];var b=d.length;if(f.substring(0,b)==d){var c=[e,f.substring(b)];return c}}return[]}function _rsasign_verifySignatureWithArgs(f,b,g,j){var e=_rsasign_getHexDigestInfoFromSig(b,g,j);var h=_rsasign_getAlgNameAndHashFromHexDisgestInfo(e);if(h.length==0){return false}var d=h[0];var i=h[1];var a=_RSASIGN_HASHHEXFUNC[d];var c=a(f);return(i==c)}function _rsasign_verifyHexSignatureForMessage(c,b){var d=parseBigInt(c,16);var a=_rsasign_verifySignatureWithArgs(b,d,this.n.toString(16),this.e.toString(16));return a}function _rsasign_verifyString(f,j){j=j.replace(_RE_HEXDECONLY,"");if(j.length!=this.n.bitLength()/4){return 0}j=j.replace(/[ \n]+/g,"");var b=parseBigInt(j,16);var i=this.doPublic(b);var e=i.toString(16).replace(/^1f+00/,"");var g=_rsasign_getAlgNameAndHashFromHexDisgestInfo(e);if(g.length==0){return false}var d=g[0];var h=g[1];var a=_RSASIGN_HASHHEXFUNC[d];var c=a(f);return(h==c)}function _rsasign_verifyStringPSS(c,p,b,k){if(p.length!==this.n.bitLength()/4){return false}var o=_RSASIGN_HASHRAWFUNC[b];var d=o(c);var h=d.length;var m=this.n.bitLength()-1;var e=Math.ceil(m/8);var f;if(k===-1){k=h}else{if((k===-2)||(k===undefined)){k=e-h-2}else{if(k<-2){throw"invalid salt length"}}}if(e<(h+k+2)){throw"data too long"}var a=this.doPublic(parseBigInt(p,16)).toByteArray();for(f=0;f<a.length;f+=1){a[f]&=255}while(a.length<e){a.unshift(0)}if(a[e-1]!==188){throw"encoded message does not end in 0xbc"}a=String.fromCharCode.apply(String,a);var r=a.substr(0,e-h-1);var n=a.substr(r.length,h);var q=(65280>>(8*e-m))&255;if((r.charCodeAt(0)&q)!==0){throw"bits beyond keysize not zero"}var j=pss_mgf1_str(n,r.length,o);var g=[];for(f=0;f<r.length;f+=1){g[f]=r.charCodeAt(f)^j.charCodeAt(f)}g[0]&=~q;var l=e-h-k-2;for(f=0;f<l;f+=1){if(g[f]!==0){throw"leftmost octets not zero"}}if(g[l]!==1){throw"0x01 marker not found"}return n===o("\x00\x00\x00\x00\x00\x00\x00\x00"+d+String.fromCharCode.apply(String,g.slice(-k)))}RSAKey.prototype.signString=_rsasign_signString;RSAKey.prototype.signStringWithSHA1=_rsasign_signStringWithSHA1;RSAKey.prototype.signStringWithSHA256=_rsasign_signStringWithSHA256;RSAKey.prototype.sign=_rsasign_signString;RSAKey.prototype.signWithSHA1=_rsasign_signStringWithSHA1;RSAKey.prototype.signWithSHA256=_rsasign_signStringWithSHA256;RSAKey.prototype.signStringPSS=_rsasign_signStringPSS;RSAKey.prototype.signPSS=_rsasign_signStringPSS;RSAKey.SALT_LEN_HLEN=-1;RSAKey.SALT_LEN_MAX=-2;RSAKey.prototype.verifyString=_rsasign_verifyString;RSAKey.prototype.verifyHexSignatureForMessage=_rsasign_verifyHexSignatureForMessage;RSAKey.prototype.verify=_rsasign_verifyString;RSAKey.prototype.verifyHexSignatureForByteArrayMessage=_rsasign_verifyHexSignatureForMessage;RSAKey.prototype.verifyStringPSS=_rsasign_verifyStringPSS;RSAKey.prototype.verifyPSS=_rsasign_verifyStringPSS;RSAKey.SALT_LEN_RECOVER=-2;