-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathjws-2.0.min.js
executable file
·3 lines (3 loc) · 4.13 KB
/
jws-2.0.min.js
1
2
3
/*! jws-2.0.3 (c) 2012 Kenji Urushima | kjur.github.com/jsjws/license
*/
if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.jws=="undefined"||!KJUR.jws){KJUR.jws={}}KJUR.jws.JWS=function(){this.isSafeJSONString=function(l,k,m){var n=null;try{n=jsonParse(l);if(typeof n!="object"){return 0}if(n.constructor===Array){return 0}if(k){k[m]=n}return 1}catch(j){return 0}};this.readSafeJSONString=function(k){var l=null;try{l=jsonParse(k);if(typeof l!="object"){return null}if(l.constructor===Array){return null}return l}catch(j){return null}};this.getEncodedSignatureValueFromJWS=function(j){if(j.match(/^[^.]+\.[^.]+\.([^.]+)$/)==null){throw"JWS signature is not a form of 'Head.Payload.SigValue'."}return RegExp.$1};this.parseJWS=function(m,o){if((this.parsedJWS!==undefined)&&(o||(this.parsedJWS.sigvalH!==undefined))){return}if(m.match(/^([^.]+)\.([^.]+)\.([^.]+)$/)==null){throw"JWS signature is not a form of 'Head.Payload.SigValue'."}var p=RegExp.$1;var k=RegExp.$2;var q=RegExp.$3;var s=p+"."+k;this.parsedJWS={};this.parsedJWS.headB64U=p;this.parsedJWS.payloadB64U=k;this.parsedJWS.sigvalB64U=q;this.parsedJWS.si=s;if(!o){var n=b64utohex(q);var l=parseBigInt(n,16);this.parsedJWS.sigvalH=n;this.parsedJWS.sigvalBI=l}var j=b64utoutf8(p);var r=b64utoutf8(k);this.parsedJWS.headS=j;this.parsedJWS.payloadS=r;if(!this.isSafeJSONString(j,this.parsedJWS,"headP")){throw"malformed JSON string for JWS Head: "+j}};function a(k,j){return utf8tob64u(k)+"."+utf8tob64u(j)}function e(l,k){var j=function(m){return KJUR.crypto.Util.hashString(m,k)};if(j==null){throw"hash function not defined in jsrsasign: "+k}return j(l)}function g(p,m,j,n,l){var o=a(p,m);var k=parseBigInt(j,16);return _rsasign_verifySignatureWithArgs(o,k,n,l)}this.verifyJWSByNE=function(l,k,j){this.parseJWS(l);return _rsasign_verifySignatureWithArgs(this.parsedJWS.si,this.parsedJWS.sigvalBI,k,j)};this.verifyJWSByKey=function(m,l){this.parseJWS(m);var j=b(this.parsedJWS.headP);var k=this.parsedJWS.headP.alg.substr(0,2)=="PS";if(l.hashAndVerify){return l.hashAndVerify(j,new Buffer(this.parsedJWS.si,"utf8").toString("base64"),b64utob64(this.parsedJWS.sigvalB64U),"base64",k)}else{if(k){return l.verifyStringPSS(this.parsedJWS.si,this.parsedJWS.sigvalH,j)}else{return l.verifyString(this.parsedJWS.si,this.parsedJWS.sigvalH)}}};this.verifyJWSByPemX509Cert=function(l,j){this.parseJWS(l);var k=new X509();k.readCertPEM(j);return k.subjectPublicKeyRSA.verifyString(this.parsedJWS.si,this.parsedJWS.sigvalH)};function b(k){var l=k.alg;var j="";if(l!="RS256"&&l!="RS512"&&l!="PS256"&&l!="PS512"){throw"JWS signature algorithm not supported: "+l}if(l.substr(2)=="256"){j="sha256"}if(l.substr(2)=="512"){j="sha512"}return j}function d(j){return b(jsonParse(j))}function i(j,o,r,l,p,q){var m=new RSAKey();m.setPrivate(l,p,q);var k=d(j);var n=m.signString(r,k);return n}function h(p,o,n,m,l){var j=null;if(typeof l=="undefined"){j=d(p)}else{j=b(l)}var k=l.alg.substr(0,2)=="PS";if(m.hashAndSign){return b64tob64u(m.hashAndSign(j,n,"binary","base64",k))}else{if(k){return hextob64u(m.signStringPSS(n,j))}else{return hextob64u(m.signString(n,j))}}}function f(o,l,n,k,m){var j=a(o,l);return i(o,l,j,n,k,m)}this.generateJWSByNED=function(q,m,p,l,o){if(!this.isSafeJSONString(q)){throw"JWS Head is not safe JSON string: "+q}var k=a(q,m);var n=i(q,m,k,p,l,o);var j=hextob64u(n);this.parsedJWS={};this.parsedJWS.headB64U=k.split(".")[0];this.parsedJWS.payloadB64U=k.split(".")[1];this.parsedJWS.sigvalB64U=j;return k+"."+j};this.generateJWSByKey=function(o,m,j){var n={};if(!this.isSafeJSONString(o,n,"headP")){throw"JWS Head is not safe JSON string: "+o}var l=a(o,m);var k=h(o,m,l,j,n.headP);this.parsedJWS={};this.parsedJWS.headB64U=l.split(".")[0];this.parsedJWS.payloadB64U=l.split(".")[1];this.parsedJWS.sigvalB64U=k;return l+"."+k};function c(p,o,n,k){var m=new RSAKey();m.readPrivateKeyFromPEMString(k);var j=d(p);var l=m.signString(n,j);return l}this.generateJWSByP1PrvKey=function(o,m,j){if(!this.isSafeJSONString(o)){throw"JWS Head is not safe JSON string: "+o}var l=a(o,m);var n=c(o,m,l,j);var k=hextob64u(n);this.parsedJWS={};this.parsedJWS.headB64U=l.split(".")[0];this.parsedJWS.payloadB64U=l.split(".")[1];this.parsedJWS.sigvalB64U=k;return l+"."+k}};