paseto-browser
Version:
In-browser JavaScript implementation of PASETO
2 lines (1 loc) • 10.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("crypto")):"function"==typeof define&&define.amd?define(["exports","crypto"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).paseto=e.paseto||{},e.paseto.v4=e.paseto.v4||{},e.paseto.v4.local={}),e.crypto)}(this,(function(e,t){"use strict";function n(e,t,n){const r=e[t]+e[n];let o=e[t+1]+e[n+1];r>=4294967296&&o++,e[t]=r,e[t+1]=o}function r(e,t,n,r){let o=e[t]+n;n<0&&(o+=4294967296);let i=e[t+1]+r;o>=4294967296&&i++,e[t]=o,e[t+1]=i}function o(e,t){return e[t]^e[t+1]<<8^e[t+2]<<16^e[t+3]<<24}function i(e,t,o,i,c,s){const f=a[c],u=a[c+1],y=a[s],d=a[s+1];n(l,e,t),r(l,e,f,u);let h=l[i]^l[e],g=l[i+1]^l[e+1];l[i]=g,l[i+1]=h,n(l,o,i),h=l[t]^l[o],g=l[t+1]^l[o+1],l[t]=h>>>24^g<<8,l[t+1]=g>>>24^h<<8,n(l,e,t),r(l,e,y,d),h=l[i]^l[e],g=l[i+1]^l[e+1],l[i]=h>>>16^g<<16,l[i+1]=g>>>16^h<<16,n(l,o,i),h=l[t]^l[o],g=l[t+1]^l[o+1],l[t]=g>>>31^h<<1,l[t+1]=h>>>31^g<<1}const c=new Uint32Array([4089235720,1779033703,2227873595,3144134277,4271175723,1013904242,1595750129,2773480762,2917565137,1359893119,725511199,2600822924,4215389547,528734635,327033209,1541459225]),s=new Uint8Array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3].map((function(e){return 2*e}))),l=new Uint32Array(32),a=new Uint32Array(32);function f(e,t){let n=0;for(n=0;n<16;n++)l[n]=e.h[n],l[n+16]=c[n];for(l[24]=l[24]^e.t,l[25]=l[25]^e.t/4294967296,t&&(l[28]=~l[28],l[29]=~l[29]),n=0;n<32;n++)a[n]=o(e.b,4*n);for(n=0;n<12;n++)i(0,8,16,24,s[16*n+0],s[16*n+1]),i(2,10,18,26,s[16*n+2],s[16*n+3]),i(4,12,20,28,s[16*n+4],s[16*n+5]),i(6,14,22,30,s[16*n+6],s[16*n+7]),i(0,10,20,30,s[16*n+8],s[16*n+9]),i(2,12,22,24,s[16*n+10],s[16*n+11]),i(4,14,16,26,s[16*n+12],s[16*n+13]),i(6,8,18,28,s[16*n+14],s[16*n+15]);for(n=0;n<16;n++)e.h[n]=e.h[n]^l[n]^l[n+16]}const u=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);function y(e,t,n,r){if(0===e||e>64)throw new Error("Illegal output length, expected 0 < length <= 64");if(t&&t.length>64)throw new Error("Illegal key, expected Uint8Array with 0 < length <= 64");if(n&&16!==n.length)throw new Error("Illegal salt, expected Uint8Array with length is 16");if(r&&16!==r.length)throw new Error("Illegal personal, expected Uint8Array with length is 16");const i={b:new Uint8Array(128),h:new Uint32Array(16),t:0,c:0,outlen:e};u.fill(0),u[0]=e,t&&(u[1]=t.length),u[2]=1,u[3]=1,n&&u.set(n,32),r&&u.set(r,48);for(let e=0;e<16;e++)i.h[e]=c[e]^o(u,4*e);return t&&(d(i,t),i.c=128),i}function d(e,t){for(let n=0;n<t.length;n++)128===e.c&&(e.t+=e.c,f(e,!1),e.c=0),e.b[e.c++]=t[n]}function h(e){for(e.t+=e.c;e.c<128;)e.b[e.c++]=0;f(e,!0);const t=new Uint8Array(e.outlen);for(let n=0;n<e.outlen;n++)t[n]=e.h[n>>2]>>8*(3&n);return t}for(let e=0;e<=255;++e)e.toString(16).padStart(2,"0");const g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",w=new Uint8Array(256);for(let e=0;e<g.length;e++)w[g.charCodeAt(e)]=e;function p(e,t=!1){let n,r,o,i,c,s=.75*e.length,l=e.length,a=0;const f=new Uint8Array(s);for(n=0;n<l;n+=4)r=w[e.charCodeAt(n)],o=w[e.charCodeAt(n+1)],i=w[e.charCodeAt(n+2)],c=w[e.charCodeAt(n+3)],f[a++]=r<<2|o>>4,f[a++]=(15&o)<<4|i>>2,f[a++]=(3&i)<<6|63&c;return t?f:(new TextDecoder).decode(f)}function A(e){let t,n=e.length,r="";for(t=0;t<n;t+=3)r+=g[e[t]>>2],r+=g[(3&e[t])<<4|e[t+1]>>4],r+=g[(15&e[t+1])<<2|e[t+2]>>6],r+=g[63&e[t+2]];return n%3==2?r.substring(0,r.length-1):n%3==1?r.substring(0,r.length-2):r}function b(e){N(Number.isSafeInteger(e),"Number too large for JavaScript to safely process");const t=e/4294967296|0,n=4294967295&e,r=new Uint8Array(8);return r[0]=255&n,r[1]=n>>>8&255,r[2]=n>>>16&255,r[3]=n>>>24&255,r[4]=255&t,r[5]=t>>>8&255,r[6]=t>>>16&255,r[7]=t>>>24&255,r}function U(e){return e[0]|e[1]<<8|e[2]<<16|e[3]<<24}function m(e,t){return U(e.slice(t,t+4))}function v(e,t,n){return n>=e.length?e:(e[n]=255&t,n+1>=e.length?e:(e[n+1]=t>>>8&255,n+2>=e.length?e:(e[n+2]=t>>>16&255,n+3>=e.length||(e[n+3]=t>>>24&255),e)))}function N(e,t="An unknown error occurred"){if(!e)throw new Error(t)}function x(...e){let t=b(e.length);for(let n of e){let e=E(n);N(e instanceof Uint8Array,"Only string and Uint8Array is allowed");let r=b(e.length);t=new Uint8Array([...t,...r,...e])}return t}function I(e=0){const n=new Uint8Array(e);if("undefined"!=typeof window){if(window.crypto&&window.crypto.getRandomValues)return window.crypto.getRandomValues(n),n;if("object"==typeof window.msCrypto&&"export function"==typeof window.msCrypto.getRandomValues)return window.msCrypto.getRandomValues(n),n}if(t.randomBytes){const r=t.randomBytes(e);return n.set(r,0),n}throw new Error("No secure random number generator available")}function E(e,t=!1){if(e instanceof Uint8Array)return e;if("string"==typeof e)return(new TextEncoder).encode(e);if(e instanceof Number&&t)return b(e);throw new Error("Unsupported type: "+typeof e)}function S(e,n){if(void 0===t.timingSafeEqual){if(N(e instanceof Uint8Array,"Must be Uint8Array"),N(n instanceof Uint8Array,"Must be Uint8Array"),e.length!==n.length)return!1;let t=0;for(let r=0;r<e.length;r++)t|=e[r]^n[r];return 0===t}return t.timingSafeEqual(E(e),E(n))}function C(e,t){return(e+t&4294967295)>>>0}function k(e,t){return((e&=4294967295)<<(t&=31)|e>>>32-t)>>>0}function T(e,t){return(4294967295&(e^t))>>>0}function j(e,t,n,r){return r=k(r^(e=e+t&4294967295),16),t=k(t^(n=n+r&4294967295),12),r=k(r^(e=e+t&4294967295),8),[e>>>0,(t=k(t^(n=n+r&4294967295),7))>>>0,n>>>0,r>>>0]}function J(e,t,n=0){return N(32===e.length,"Invalid key size"),N(12===t.length,"Invalid nonce length"),n=function(e){N(Number.isSafeInteger(e),"Number too large for JavaScript to safely process");const t=4294967295&e,n=new Uint8Array(4);return n[0]=255&t,n[1]=t>>>8&255,n[2]=t>>>16&255,n[3]=t>>>24&255,n}(n),new Uint8Array([101,120,112,97,110,100,32,51,50,45,98,121,116,101,32,107].concat([...e,...n,...t]))}function O(e,t,n,r){return N(32===n.length,`Key must be 32 bytes; ${n.length} provided`),N(12===t.length,"Nonce must be 12 bytes"),function(e,t){let n,r,o,i,c,s,l,a,f,u,y,d,h,g,w,p,A,b=m(e,0),N=m(e,4),x=m(e,8),I=m(e,12),E=m(e,16),S=m(e,20),k=m(e,24),J=m(e,28),O=m(e,32),V=m(e,36),K=m(e,40),R=m(e,44),q=m(e,48),D=m(e,52),M=m(e,56),P=m(e,60),z=0,B=t.length,F=new Uint8Array(B),L=new Uint8Array(64);for(;z<B;){A=z+64>=B?B:z+64,L.fill(0,0);let e=0;for(let n=z;n<A;n++)L[e++]=t[n];n=b,r=N,o=x,i=I,c=E,s=S,l=k,a=J,f=O,u=V,y=K,d=R,h=q,g=D,w=M,p=P;for(let e=0;e<10;e++)[n,c,f,h]=j(n,c,f,h),[r,s,u,g]=j(r,s,u,g),[o,l,y,w]=j(o,l,y,w),[i,a,d,p]=j(i,a,d,p),[n,s,y,p]=j(n,s,y,p),[r,l,d,h]=j(r,l,d,h),[o,a,f,g]=j(o,a,f,g),[i,c,u,w]=j(i,c,u,w);n=C(n,b),r=C(r,N),o=C(o,x),i=C(i,I),c=C(c,E),s=C(s,S),l=C(l,k),a=C(a,J),f=C(f,O),u=C(u,V),y=C(y,K),d=C(d,R),h=C(h,q),g=C(g,D),w=C(w,M),p=C(p,P),n=T(n,U(L.slice(0,4))),r=T(r,U(L.slice(4,8))),o=T(o,U(L.slice(8,12))),i=T(i,U(L.slice(12,16))),c=T(c,U(L.slice(16,20))),s=T(s,U(L.slice(20,24))),l=T(l,U(L.slice(24,28))),a=T(a,U(L.slice(28,32))),f=T(f,U(L.slice(32,36))),u=T(u,U(L.slice(36,40))),y=T(y,U(L.slice(40,44))),d=T(d,U(L.slice(44,48))),h=T(h,U(L.slice(48,52))),g=T(g,U(L.slice(52,56))),w=T(w,U(L.slice(56,60))),p=T(p,U(L.slice(60,64))),F=v(F,n,z),F=v(F,r,z+4),F=v(F,o,z+8),F=v(F,i,z+12),F=v(F,c,z+16),F=v(F,s,z+20),F=v(F,l,z+24),F=v(F,a,z+28),F=v(F,f,z+32),F=v(F,u,z+36),F=v(F,y,z+40),F=v(F,d,z+44),F=v(F,h,z+48),F=v(F,g,z+52),F=v(F,w,z+56),F=v(F,p,z+60),q++,z+=64}return F.slice(0,B)}(J(n,t,r),e)}function V(e,t){const n=function(e,t){return N(32===e.length,"Invalid key size"),N(16===t.length,"Invalid nonce length"),new Uint8Array([101,120,112,97,110,100,32,51,50,45,98,121,116,101,32,107].concat([...e,...t]))}(t,e);let r=m(n,0),o=m(n,4),i=m(n,8),c=m(n,12),s=m(n,16),l=m(n,20),a=m(n,24),f=m(n,28),u=m(n,32),y=m(n,36),d=m(n,40),h=m(n,44),g=m(n,48),w=m(n,52),p=m(n,56),A=m(n,60);for(let e=0;e<10;e++)[r,s,u,g]=j(r,s,u,g),[o,l,y,w]=j(o,l,y,w),[i,a,d,p]=j(i,a,d,p),[c,f,h,A]=j(c,f,h,A),[r,l,d,A]=j(r,l,d,A),[o,a,h,g]=j(o,a,h,g),[i,f,u,w]=j(i,f,u,w),[c,s,y,p]=j(c,s,y,p);const b=new Uint8Array(32);return v(b,r,0),v(b,o,4),v(b,i,8),v(b,c,12),v(b,g,16),v(b,w,20),v(b,p,24),v(b,A,28),b}function K(e,t,n,r=1){N(32===n.length,`Key must be 32 bytes; ${n.length} provided`),N(24===t.length,"Nonce must be 24 bytes");const o=new Uint8Array(12);return o.set(t.slice(16,24),4),O(e,o,V(t.slice(0,16),n),r)}const R="v4.local.",q=new TextEncoder,D=q.encode(R),M=q.encode("paseto-encryption-key"),P=q.encode("paseto-auth-key-for-aead");class z{constructor(e){N(e instanceof Uint8Array,"Input must be a Uint8Array"),N(32===e.length,"Key must be 32 bytes"),this.bytes=e}static generate(){const e=I(32);return new z(e)}async decode(e,t=""){return JSON.parse(await this.decrypt(e,t))}async encode(e,t="",n=""){return"object"==typeof t&&(t=JSON.stringify(t)),"string"==typeof t&&(t=E(t)),this.encrypt(JSON.stringify(e),t,n)}async assertFooter(e,t){const n=e.split(".");N(4===n.length,"No footer provided");return S(p(n[3],t instanceof Uint8Array),t)}static getFooter(e,t=!1){const n=e.split(".");N(4===n.length,"No footer provided");const r=p(n[3],t);return t?JSON.parse(function(e){if("string"==typeof e)return e;if(e instanceof Uint8Array)return(new TextDecoder).decode(e);throw new Error("Unsupported type: "+typeof e)}(r)):r}getKey(){return this.bytes}async encrypt(e,t="",n=""){const r=I(32);let o;o=y(56,this.bytes),d(o,M),d(o,r);const i=h(o),c=i.slice(0,32),s=i.slice(32);o=y(32,this.bytes),d(o,P),d(o,r);const l=h(o),a=await K(E(e),s,c,0);o=y(32,l),d(o,x(D,r,a,t,n));const f=A(function(...e){let t=0;for(const n of e)n.length&&(t+=n.length);const n=new Uint8Array(t);let r=0;for(const t of e)n.set(t,r),r+=t.length;return n}(r,a,h(o)));return t.length>0?[R.slice(0,8),f,A(t)].join("."):[R.slice(0,8),f].join(".")}async decrypt(e,t=""){const{n:n,c:r,t:o,footer:i}=await this.decompose(e);let c;c=y(56,this.bytes),d(c,M),d(c,n);const s=h(c),l=s.slice(0,32),a=s.slice(32);c=y(32,this.bytes),d(c,P),d(c,n);c=y(32,h(c)),d(c,x(D,n,r,i,t));N(S(o,h(c)),"Invalid tag");const f=await K(r,a,l,0);return(new TextDecoder).decode(f)}async decompose(e){N(S(E(e.slice(0,9)),D),"Invalid token");const t=e.split("."),n=p(t[2],!0),r=n.length;return{n:n.slice(0,32),c:n.slice(32,r-32),t:n.slice(r-32),footer:t.length>3?p(t[3]):new Uint8Array(0)}}}"undefined"!=typeof window&&(window.PasetoV4Local=z),e.PasetoV4Local=z,Object.defineProperty(e,"__esModule",{value:!0})}));//# sourceMappingURL=paseto.v4.local.js.map