1function V(e){return new TextEncoder().encode(e)}function be(e){return new TextDecoder("utf-8",{fatal:!1}).decode(e)}function ke(e){let n="";for(let c=0;c<e.length;c+=32768){const o=e.subarray(c,c+32768);n+=String.fromCharCode(...o)}return btoa(n)}function Be(e){const n=atob(e),r=new Uint8Array(n.length);for(let c=0;c<n.length;c+=1)r[c]=n.charCodeAt(c);return r}function oe(e){const n=typeof e=="string"?V(e):e;return ke(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}function ge(e){const n=String(e).replace(/-/g,"+").replace(/_/g,"/").trim(),r=n.length%4===0?"":"=".repeat(4-n.length%4);return Be(n+r)}function le(e){return be(ge(e))}function P(e){const n=String(e??"");if(n.trim()==="")return{ok:!1,error:"JSON is empty."};try{return{ok:!0,value:JSON.parse(n)}}catch(r){return{ok:!1,error:r instanceof Error?r.message:"Invalid JSON."}}}function D(e){try{return JSON.stringify(e,null,2)}catch{return""}}function he(e){const n=String(e??"").trim();if(n==="")return{ok:!1,error:"JWT is empty."};const r=n.split(".");if(r.length!==3)return{ok:!1,error:"JWT must have exactly 3 sections separated by dots."};const[c,o,d]=r;if(!c||!o)return{ok:!1,error:"JWT header and payload must be present."};let u="",w="";try{u=le(c)}catch{return{ok:!1,error:"Header is not valid Base64URL."}}try{w=le(o)}catch{return{ok:!1,error:"Payload is not valid Base64URL."}}const g=P(u);if(!g.ok)return{ok:!1,error:`Header JSON error: ${g.error}`};const y=P(w);return y.ok?{ok:!0,headerB64:c,payloadB64:o,signatureB64:d||"",headerJson:g.value,payloadJson:y.value}:{ok:!1,error:`Payload JSON error: ${y.error}`}}function xe(e){return e==="HS256"?"SHA-256":e==="HS384"?"SHA-384":"SHA-512"}async function ye(e,n){const r=V(String(e??""));return crypto.subtle.importKey("raw",r,{name:"HMAC",hash:{name:xe(n)}},!1,["sign","verify"])}async function Ie(e,n,r){const c=String(e??"").trim();if(c==="")return{ok:!1,error:"JWT is empty."};const o=c.split(".");if(o.length!==3)return{ok:!1,error:"JWT must have 3 parts to verify."};const[d,u,w]=o;if(!d||!u||!w)return{ok:!1,error:"JWT header, payload, and signature are required for verification."};if(!n)return{ok:!1,error:"Secret is required for HS* verification."};if(!(crypto!=null&&crypto.subtle))return{ok:!1,error:"Web Crypto API is not available in this browser context."};let g;try{g=ge(w)}catch{return{ok:!1,error:"Signature is not valid Base64URL."}}const y=V(`${d}.${u}`);try{const k=await ye(n,r);return{ok:!0,verified:await crypto.subtle.verify("HMAC",k,g,y)}}catch(k){return{ok:!1,error:k instanceof Error?k.message:"Signature verification failed."}}}async function Te(e,n,r,c){if(!(crypto!=null&&crypto.subtle))return{ok:!1,error:"Web Crypto API is not available in this browser context."};if(!r)return{ok:!1,error:"Secret is required for HS* signing."};const o=V(`${e}.${n}`);try{const d=await ye(r,c),u=await crypto.subtle.sign("HMAC",d,o);return{ok:!0,signatureB64:oe(new Uint8Array(u))}}catch(d){return{ok:!1,error:d instanceof Error?d.message:"Signing failed."}}}async function j(e,n,r,c){const o=typeof e=="object"&&e?{...e}:e;o&&typeof o=="object"&&(o.alg=c,o.typ==null&&(o.typ="JWT"));const d=JSON.stringify(o),u=JSON.stringify(n),w=oe(d),g=oe(u),y=await Te(w,g,r,c);return y.ok?{ok:!0,jwt:`${w}.${g}.${y.signatureB64}`,headerB64:w,payloadB64:g}:y}function Ae(e){return String(e).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function fe(e){return Ae(e).replace(/("(?:\\u[a-fA-F0-9]{4}|\\[^u]|[^\\"])*"(?:\s*:)?|\btrue\b|\bfalse\b|\bnull\b|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|[{}[\],:])/g,r=>`<span class="${r[0]==='"'&&r.endsWith(":")?"ct-jwt-json__key":r[0]==='"'?"ct-jwt-json__str":r==="true"||r==="false"?"ct-jwt-json__bool":r==="null"?"ct-jwt-json__null":/^-?\d/.test(r)?"ct-jwt-json__num":"ct-jwt-json__pun"}">${r}</span>`)}function a(e,n){e&&(e.textContent=n)}function me(e,n){e&&(e.innerHTML=n)}function i(e,n,r){e&&(e.className=`badge text-bg-${n}`,e.textContent=r)}async function F(e){const n=String(e??"");if(!n)return!1;try{return await navigator.clipboard.writeText(n),!0}catch{const r=document.createElement("textarea");r.value=n,r.style.position="fixed",r.style.left="-9999px",document.body.appendChild(r),r.focus(),r.select();let c=!1;try{c=document.execCommand("copy")}catch{c=!1}finally{document.body.removeChild(r)}return c}}function ne(){return Math.floor(Date.now()/1e3)}function re(e){if(typeof e!="number"||!Number.isFinite(e))return null;try{const n=new Date(e*1e3);
1return Number.isNaN(n.getTime())?null:n.toLocaleString(void 0,{year:"numeric",month:"short",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"})}catch{return null}}function He(){var de;if(!document.getElementById("ct-jwt-root"))return;const n=document.getElementById("ctJwtPresetValid"),r=document.getElementById("ctJwtPresetExpired"),c=document.getElementById("ctJwtPresetBadSig"),o=document.getElementById("ctJwtInput"),d=document.getElementById("ctJwtSecret"),u=document.getElementById("ctJwtAlg"),w=document.getElementById("ctJwtErrors"),g=document.getElementById("ctJwtHeaderOut"),y=document.getElementById("ctJwtPayloadOut"),k=document.getElementById("ctJwtSigOut"),R=document.getElementById("ctJwtHeaderErr"),we=document.getElementById("ctJwtPayloadErr"),I=document.getElementById("ctJwtBadgeStructure"),x=document.getElementById("ctJwtBadgeDecoded"),B=document.getElementById("ctJwtBadgeSig"),T=document.getElementById("ctJwtCopyJwt"),A=document.getElementById("ctJwtCopyHeader"),H=document.getElementById("ctJwtCopyPayload"),M=document.getElementById("ctJwtClearAll"),ce=document.getElementById("ctJwtClaimIat"),se=document.getElementById("ctJwtClaimNbf"),ae=document.getElementById("ctJwtClaimExp"),W=document.getElementById("ctJwtClaimStatus"),N=document.getElementById("ctJwtExpBadge"),L=document.getElementById("ctJwtGenAlg"),q=document.getElementById("ctJwtGenSecret"),p=document.getElementById("ctJwtGenHeader"),v=document.getElementById("ctJwtGenPayload"),_=document.getElementById("ctJwtGenHeaderErr"),G=document.getElementById("ctJwtGenPayloadErr"),ie=document.getElementById("ctJwtGenErrors"),J=document.getElementById("ctJwtGenerated"),O=document.getElementById("ctJwtFmtHeader"),z=document.getElementById("ctJwtFmtPayload"),K=document.getElementById("ctJwtGenerate"),C=document.getElementById("ctJwtCopyGenerated"),$=document.getElementById("ctJwtUseInDecoder"),Ee={alg:"HS256",typ:"JWT"},pe={sub:"1234567890",name:"John Doe",iat:ne()};p&&(p.value=D(Ee)),v&&(v.value=D(pe));const ve=t=>{const m=ne(),s=typeof(t==null?void 0:t.iat)=="number"?t.iat:Number(t==null?void 0:t.iat),f=typeof(t==null?void 0:t.nbf)=="number"?t.nbf:Number(t==null?void 0:t.nbf),l=typeof(t==null?void 0:t.exp)=="number"?t.exp:Number(t==null?void 0:t.exp),S=Number.isFinite(s)?re(s):null,b=Number.isFinite(f)?re(f):null,E=Number.isFinite(l)?re(l):null;if(a(ce,S||"â"),a(se,b||"â"),a(ae,E||"â"),!Number.isFinite(l)){i(N,"secondary","Expiration"),a(W,"No exp claim found. Many APIs require exp for security.");return}if(l<m){i(N,"danger","Expired"),a(W,`Token expired ${Math.max(0,m-l)}s ago.`);return}const h=l-m;i(N,h<300?"warning":"success",h<300?"Expiring soon":"Valid"),a(W,`Token expires in ${h}s.`)},Y=t=>{T&&(T.disabled=!t),A&&(A.disabled=!t),H&&(H.disabled=!t)},ue=()=>{a(g,""),a(y,""),k&&(k.value=""),a(R,""),a(we,""),a(w,""),a(ce,"â"),a(se,"â"),a(ae,"â"),a(W,"Paste a JWT to inspect claims."),i(N,"secondary","Expiration"),Y(!1)},Q=async()=>{var l;const t=((l=o==null?void 0:o.value)==null?void 0:l.trim())||"",m=(d==null?void 0:d.value)||"",s=(u==null?void 0:u.value)||"HS256";if(!t){i(B,"secondary","Signature");return}if(!m){i(B,"secondary","Signature Not Verified");return}i(B,"warning","Verifyingâ¦");const f=await Ie(t,m,s);if(!f.ok){i(B,"danger","Signature Verification Failed");return}i(B,f.verified?"success":"danger",f.verified?"Signature Verified":"Signature Verification Failed")},X=async()=>{var b,E;const t=((b=o==null?void 0:o.value)==null?void 0:b.trim())||"";if(ue(),!t){i(I,"secondary","Structure"),i(x,"secondary","Decoded"),i(B,"secondary","Signature");return}if(t.split(".").length!==3){i(I,"danger","Invalid JWT"),i(x,"secondary","Decoded"),i(B,"secondary","Signature"),a(w,"JWT must contain 3 sections separated by dots.");return}i(I,"success","Valid JWT");const s=he(t);if(!s.ok){i(x,"danger","Invalid JWT"),a(w,s.error);return}i(x,"success","Decoded Successfully"),Y(!0);const f=D(s.headerJson),l=D(s.payloadJson);me(g,fe(f)),me(y,fe(l)),k&&(k.value=s.signatureB64||""),ve(s.payloadJson);const S=(E=s.headerJson)==null?void 0:E.alg;u&&(S==="HS256"||S==="HS384"||S==="HS512")&&(u.value=S),await Q()},U=(t,m)=>{if(!t)return;const s=P(t.value);if(!s.ok){a(m,s.error);return}a(m,""),t.value=D(s.value)},Z=t=>{C&&(C.disabled=!t),$&&($.disabled=!t)},Je=async()=>{a(ie,""),a(_,""),a(G,""),J&&(J.value=""),Z(!1);const t=(L==null?void 0:L.value)||"HS256",m=(q==null?void 0:q.value)||"",s=P((p==null?void 0:p.value)||"");if(!s.ok){a(_,s.error);return}const f=P((v==null?void 0:v.value)||"");if(!f.ok){a(G,f.error);return}const l=await j(s.value,f.value,m,t);if(!l.ok){a(ie,l.error);return}J&&(J.value=l.jwt),Z(!0)};o==null||o.addEventListener("input",()=>{X()}),d==null||d.addEventListener("input",()=>Q()),u==null||u.addEventListener("change",()=>Q()),T==null||T.addEventListener("click",async()=>{await F((o==null?void 0:o.value)||"")}),A==null||A.addEventListener("click",async()=>{await F((g==null?void 0:g.textContent)||"")}),H==null||H.addEventListener("click",async()=>{await F((y==null?void 0:y.textContent)||"")}),M==null||M.addEventListener("click",()=>{o&&(o.value=""),d&&(d.value=""),ue(),i(I,"secondary","Structure"),i(x,"secondary","Decoded"),i(B,"secondary","Signature")});const ee=async t=>{if(!o)return;const m="demo-secret",s="HS256",f=ne(),l={alg:s,typ:"JWT"},S={iss:"conversiontab",aud:"developer",sub:"user_123",scope:"read:profile write:profile",iat:f,nbf:f-10};if(d&&(d.value=m),u&&(u.value=s),t==="valid"){const b={...S,exp:f+3600,name:"Valid Token Example"},E=await j(l,b,m,s);E.ok&&(o.value=E.jwt)}else if(t==="expired"){const b={...S,exp:f-120,name:"Expired Token Example"},E=await j(l,b,m,s);E.ok&&(o.value=E.jwt)}else if(t==="badSig"){const b={...S,exp:f+3600,name:"Invalid Signature Example"},E=await j(l,b,m,s);if(E.ok){const h=E.jwt.split("."),te=h[2]||"",Se=te.length>6?`${te.slice(0,-6)}AAAAAA`:`${te}AAAAAA`;o.value=`${h[0]}.${h[1]}.${Se}`}}await X(),o.focus(),o.setSelectionRange(0,0)};n==null||n.addEventListener("click",()=>ee("valid")),r==null||r.addEventListener("click",()=>ee("expired")),c==null||c.addEventListener("click",()=>ee("badSig")),O==null||O.addEventListener("click",()=>U(p,_)),z==null||z.addEventListener("click",()=>U(v,G)),p==null||p.addEventListener("blur",()=>U(p,_)),v==null||v.addEventListener("blur",()=>U(v,G)),K==null||K.addEventListener("click",()=>Je()),C==null||C.addEventListener("click",async()=>{await F((J==null?void 0:J.value)||"")}),$==null||$.addEventListener("click",async()=>{const t=(J==null?void 0:J.value)||"";!t||!o||(o.value=t,u&&L&&(u.value=L.value),await X(),o.focus(),o.setSelectionRange(0,0))}),i(I,"secondary","Structure"),i(x,"secondary","Decoded"),i(B,"secondary","Signature"),i(N,"secondary","Expiration"),Y(!1),Z(!1),(de=window.__ctRenderPendingYandexRtbSlots)==null||de.call(window)}export{He as initJwtDecoderGeneratorPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.