tweetnacljs
Version:
Port of TweetNaCl cryptographic library to JavaScript
1 lines • 19.2 kB
JavaScript
!function(r){"use strict";function n(r,n){return r<<n|r>>>32-n}function e(r,n){var e=255&r[n+3];return e=e<<8|255&r[n+2],e=e<<8|255&r[n+1],e<<8|255&r[n+0]}function t(r,n){var e=r[n]<<24|r[n+1]<<16|r[n+2]<<8|r[n+3],t=r[n+4]<<24|r[n+5]<<16|r[n+6]<<8|r[n+7];return new hn(e,t)}function o(r,n,e){var t;for(t=0;4>t;t++)r[n+t]=255&e,e>>>=8}function i(r,n,e){r[n]=e.hi>>24&255,r[n+1]=e.hi>>16&255,r[n+2]=e.hi>>8&255,r[n+3]=255&e.hi,r[n+4]=e.lo>>24&255,r[n+5]=e.lo>>16&255,r[n+6]=e.lo>>8&255,r[n+7]=255&e.lo}function a(r,n,e,t,o){var i,a=0;for(i=0;o>i;i++)a|=r[n+i]^e[t+i];return(1&a-1>>>8)-1}function f(r,n,e,t){return a(r,n,e,t,16)}function u(r,n,e,t){return a(r,n,e,t,32)}function c(r,t,i,a,f){var u,c,w,y=new Uint32Array(16),s=new Uint32Array(16),l=new Uint32Array(16),h=new Uint32Array(4);for(u=0;4>u;u++)s[5*u]=e(a,4*u),s[1+u]=e(i,4*u),s[6+u]=e(t,4*u),s[11+u]=e(i,16+4*u);for(u=0;16>u;u++)l[u]=s[u];for(u=0;20>u;u++){for(c=0;4>c;c++){for(w=0;4>w;w++)h[w]=s[(5*c+4*w)%16];for(h[1]^=n(h[0]+h[3]|0,7),h[2]^=n(h[1]+h[0]|0,9),h[3]^=n(h[2]+h[1]|0,13),h[0]^=n(h[3]+h[2]|0,18),w=0;4>w;w++)y[4*c+(c+w)%4]=h[w]}for(w=0;16>w;w++)s[w]=y[w]}if(f){for(u=0;16>u;u++)s[u]=s[u]+l[u]|0;for(u=0;4>u;u++)s[5*u]=s[5*u]-e(a,4*u)|0,s[6+u]=s[6+u]-e(t,4*u)|0;for(u=0;4>u;u++)o(r,4*u,s[5*u]),o(r,16+4*u,s[6+u])}else for(u=0;16>u;u++)o(r,4*u,s[u]+l[u]|0)}function w(r,n,e,t){return c(r,n,e,t,!1),0}function y(r,n,e,t){return c(r,n,e,t,!0),0}function s(r,n,e,t,o,i,a){var f,u,c=new Uint8Array(16),y=new Uint8Array(64);if(!o)return 0;for(u=0;16>u;u++)c[u]=0;for(u=0;8>u;u++)c[u]=i[u];for(;o>=64;){for(w(y,c,a,Sn),u=0;64>u;u++)r[n+u]=(e?e[t+u]:0)^y[u];for(f=1,u=8;16>u;u++)f=f+(255&c[u])|0,c[u]=255&f,f>>>=8;o-=64,n+=64,e&&(t+=64)}if(o>0)for(w(y,c,a,Sn),u=0;o>u;u++)r[n+u]=(e?e[t+u]:0)^y[u];return 0}function l(r,n,e,t,o){return s(r,n,null,0,e,t,o)}function h(r,n,e,t,o){var i=new Uint8Array(32);return y(i,t,o,Sn),l(r,n,e,t.subarray(16),i)}function g(r,n,e,t,o,i,a){var f=new Uint8Array(32);return y(f,i,a,Sn),s(r,n,e,t,o,i.subarray(16),f)}function v(r,n){var e,t=0;for(e=0;17>e;e++)t=t+(r[e]+n[e]|0)|0,r[e]=255&t,t>>>=8}function b(r,n,e,t,o,i){var a,f,u,c,w=new Uint32Array(17),y=new Uint32Array(17),s=new Uint32Array(17),l=new Uint32Array(17),h=new Uint32Array(17);for(u=0;17>u;u++)y[u]=s[u]=0;for(u=0;16>u;u++)y[u]=i[u];for(y[3]&=15,y[4]&=252,y[7]&=15,y[8]&=252,y[11]&=15,y[12]&=252,y[15]&=15;o>0;){for(u=0;17>u;u++)l[u]=0;for(u=0;16>u&&o>u;++u)l[u]=e[t+u];for(l[u]=1,t+=u,o-=u,v(s,l),f=0;17>f;f++)for(w[f]=0,u=0;17>u;u++)w[f]=w[f]+s[u]*(f>=u?y[f-u]:320*y[f+17-u]|0)|0|0;for(f=0;17>f;f++)s[f]=w[f];for(c=0,u=0;16>u;u++)c=c+s[u]|0,s[u]=255&c,c>>>=8;for(c=c+s[16]|0,s[16]=3&c,c=5*(c>>>2)|0,u=0;16>u;u++)c=c+s[u]|0,s[u]=255&c,c>>>=8;c=c+s[16]|0,s[16]=c}for(u=0;17>u;u++)h[u]=s[u];for(v(s,Kn),a=0|-(s[16]>>>7),u=0;17>u;u++)s[u]^=a&(h[u]^s[u]);for(u=0;16>u;u++)l[u]=i[u+16];for(l[16]=0,v(s,l),u=0;16>u;u++)r[n+u]=s[u];return 0}function p(r,n,e,t,o,i){var a=new Uint8Array(16);return b(a,0,e,t,o,i),f(r,n,a,0)}function A(r,n,e,t,o){var i;if(32>e)return-1;for(g(r,0,n,0,e,t,o),b(r,16,r,32,e-32,r),i=0;16>i;i++)r[i]=0;return 0}function _(r,n,e,t,o){var i,a=new Uint8Array(32);if(32>e)return-1;if(h(a,0,32,t,o),0!==p(n,16,n,32,e-32,a))return-1;for(g(r,0,n,0,e,t,o),i=0;32>i;i++)r[i]=0;return 0}function U(r,n){var e;for(e=0;16>e;e++)r[e]=0|n[e]}function d(r){var n,e;for(e=0;16>e;e++)r[e]+=65536,n=Math.floor(r[e]/65536),r[(e+1)*(15>e?1:0)]+=n-1+37*(n-1)*(15===e?1:0),r[e]-=65536*n}function E(r,n,e){for(var t,o=~(e-1),i=0;16>i;i++)t=o&(r[i]^n[i]),r[i]^=t,n[i]^=t}function x(r,n){var e,t,o,i=gn(),a=gn();for(e=0;16>e;e++)a[e]=n[e];for(d(a),d(a),d(a),t=0;2>t;t++){for(i[0]=a[0]-65517,e=1;15>e;e++)i[e]=a[e]-65535-(i[e-1]>>16&1),i[e-1]&=65535;i[15]=a[15]-32767-(i[14]>>16&1),o=i[15]>>16&1,i[14]&=65535,E(a,i,1-o)}for(e=0;16>e;e++)r[2*e]=255&a[e],r[2*e+1]=a[e]>>8}function m(r,n){var e=new Uint8Array(32),t=new Uint8Array(32);return x(e,r),x(t,n),u(e,0,t,0)}function B(r){var n=new Uint8Array(32);return x(n,r),1&n[0]}function S(r,n){var e;for(e=0;16>e;e++)r[e]=n[2*e]+(n[2*e+1]<<8);r[15]&=32767}function K(r,n,e){var t;for(t=0;16>t;t++)r[t]=n[t]+e[t]|0}function T(r,n,e){var t;for(t=0;16>t;t++)r[t]=n[t]-e[t]|0}function Y(r,n,e){var t,o,i=new Float64Array(31);for(t=0;31>t;t++)i[t]=0;for(t=0;16>t;t++)for(o=0;16>o;o++)i[t+o]+=n[t]*e[o];for(t=0;15>t;t++)i[t]+=38*i[t+16];for(t=0;16>t;t++)r[t]=i[t];d(r),d(r)}function C(r,n){Y(r,n,n)}function L(r,n){var e,t=gn();for(e=0;16>e;e++)t[e]=n[e];for(e=253;e>=0;e--)C(t,t),2!==e&&4!==e&&Y(t,t,n);for(e=0;16>e;e++)r[e]=t[e]}function R(r,n){var e,t=gn();for(e=0;16>e;e++)t[e]=n[e];for(e=250;e>=0;e--)C(t,t),1!==e&&Y(t,t,n);for(e=0;16>e;e++)r[e]=t[e]}function k(r,n,e){var t,o,i=new Uint8Array(32),a=new Float64Array(80),f=gn(),u=gn(),c=gn(),w=gn(),y=gn(),s=gn();for(o=0;31>o;o++)i[o]=n[o];for(i[31]=127&n[31]|64,i[0]&=248,S(a,e),o=0;16>o;o++)u[o]=a[o],w[o]=f[o]=c[o]=0;for(f[0]=w[0]=1,o=254;o>=0;--o)t=i[o>>>3]>>>(7&o)&1,E(f,u,t),E(c,w,t),K(y,f,c),T(f,f,c),K(c,u,w),T(u,u,w),C(w,y),C(s,f),Y(f,c,f),Y(c,u,y),K(y,f,c),T(f,f,c),C(u,f),T(c,w,s),Y(f,c,Un),K(f,f,w),Y(c,c,f),Y(f,w,s),Y(w,u,a),C(u,y),E(f,u,t),E(c,w,t);for(o=0;16>o;o++)a[o+16]=f[o],a[o+32]=c[o],a[o+48]=u[o],a[o+64]=w[o];var l=a.subarray(32),h=a.subarray(16);return L(l,l),Y(h,h,l),x(r,h),0}function z(r,n){return k(r,n,pn)}function P(r,n){return vn(n,32),z(r,n)}function F(r,n,e){var t=new Uint8Array(32);return k(t,e,n),y(r,bn,t,Sn)}function N(r,n,e,t,o,i){var a=new Uint8Array(32);return F(a,o,i),Tn(r,n,e,t,a)}function O(r,n,e,t,o,i){var a=new Uint8Array(32);return F(a,o,i),Yn(r,n,e,t,a)}function M(){var r,n,e,t=0,o=0,i=0,a=0,f=65535;for(e=0;e<arguments.length;e++)r=arguments[e].lo,n=arguments[e].hi,t+=r&f,o+=r>>>16,i+=n&f,a+=n>>>16;return o+=t>>>16,i+=o>>>16,a+=i>>>16,new hn(i&f|a<<16,t&f|o<<16)}function G(r,n){return new hn(r.hi>>>n,r.lo>>>n|r.hi<<32-n)}function I(){var r,n=0,e=0;for(r=0;r<arguments.length;r++)n^=arguments[r].lo,e^=arguments[r].hi;return new hn(e,n)}function Z(r,n){var e,t,o=32-n;return 32>n?(e=r.hi>>>n|r.lo<<o,t=r.lo>>>n|r.hi<<o):64>n&&(e=r.lo>>>n|r.hi<<o,t=r.hi>>>n|r.lo<<o),new hn(e,t)}function j(r,n,e){var t=r.hi&n.hi^~r.hi&e.hi,o=r.lo&n.lo^~r.lo&e.lo;return new hn(t,o)}function V(r,n,e){var t=r.hi&n.hi^r.hi&e.hi^n.hi&e.hi,o=r.lo&n.lo^r.lo&e.lo^n.lo&e.lo;return new hn(t,o)}function q(r){return I(Z(r,28),Z(r,34),Z(r,39))}function X(r){return I(Z(r,14),Z(r,18),Z(r,41))}function D(r){return I(Z(r,1),Z(r,8),G(r,7))}function H(r){return I(Z(r,19),Z(r,61),G(r,6))}function J(r,n,e){var o,a,f,u=[],c=[],w=[],y=[];for(a=0;8>a;a++)u[a]=w[a]=t(r,8*a);for(var s=0;e>=128;){for(a=0;16>a;a++)y[a]=t(n,8*a+s);for(a=0;80>a;a++){for(f=0;8>f;f++)c[f]=w[f];for(o=M(w[7],X(w[4]),j(w[4],w[5],w[6]),Cn[a],y[a%16]),c[7]=M(o,q(w[0]),V(w[0],w[1],w[2])),c[3]=M(c[3],o),f=0;8>f;f++)w[(f+1)%8]=c[f];if(a%16===15)for(f=0;16>f;f++)y[f]=M(y[f],y[(f+9)%16],D(y[(f+1)%16]),H(y[(f+14)%16]))}for(a=0;8>a;a++)w[a]=M(w[a],u[a]),u[a]=w[a];s+=128,e-=128}for(a=0;8>a;a++)i(r,8*a,u[a]);return e}function Q(r,n,e){var t,o=new Uint8Array(64),a=new Uint8Array(256),f=e;for(t=0;64>t;t++)o[t]=Ln[t];for(J(o,n,e),e%=128,t=0;256>t;t++)a[t]=0;for(t=0;e>t;t++)a[t]=n[f-e+t];for(a[e]=128,e=256-128*(112>e?1:0),a[e-9]=0,i(a,e-8,new hn(f/536870912|0,f<<3)),J(o,a,e),t=0;64>t;t++)r[t]=o[t];return 0}function W(r,n){var e=gn(),t=gn(),o=gn(),i=gn(),a=gn(),f=gn(),u=gn(),c=gn(),w=gn();T(e,r[1],r[0]),T(w,n[1],n[0]),Y(e,e,w),K(t,r[0],r[1]),K(w,n[0],n[1]),Y(t,t,w),Y(o,r[3],n[3]),Y(o,o,En),Y(i,r[2],n[2]),K(i,i,i),T(a,t,e),T(f,i,o),K(u,i,o),K(c,t,e),Y(r[0],a,f),Y(r[1],c,u),Y(r[2],u,f),Y(r[3],a,c)}function $(r,n,e){var t;for(t=0;4>t;t++)E(r[t],n[t],e)}function rn(r,n){var e=gn(),t=gn(),o=gn();L(o,n[2]),Y(e,n[0],o),Y(t,n[1],o),x(r,t),r[31]^=B(e)<<7}function nn(r,n,e){var t,o;for(U(r[0],An),U(r[1],_n),U(r[2],_n),U(r[3],An),o=255;o>=0;--o)t=e[o/8|0]>>(7&o)&1,$(r,n,t),W(n,r),W(r,r),$(r,n,t)}function en(r,n){var e=[gn(),gn(),gn(),gn()];U(e[0],xn),U(e[1],mn),U(e[2],_n),Y(e[3],xn,mn),nn(r,e,n)}function tn(r,n,e){var t,o=new Uint8Array(64),i=[gn(),gn(),gn(),gn()];for(Q(o,r,32),o[0]&=248,o[31]&=127,o[31]|=64,en(i,o),rn(n,i),t=0;32>t;t++)e[t]=r[t];for(t=0;32>t;t++)e[t+32]=n[t];return 0}function on(r,n){var e=new Uint8Array(Xn);return vn(e,Xn),tn(e,r,n)}function an(r,n){var e,t,o,i;for(t=63;t>=32;--t){for(e=0,o=t-32,i=t-12;i>o;++o)n[o]+=e-16*n[t]*Rn[o-(t-32)],e=n[o]+128>>8,n[o]-=256*e;n[o]+=e,n[t]=0}for(e=0,o=0;32>o;o++)n[o]+=e-(n[31]>>4)*Rn[o],e=n[o]>>8,n[o]&=255;for(o=0;32>o;o++)n[o]-=e*Rn[o];for(t=0;32>t;t++)n[t+1]+=n[t]>>8,r[t]=255&n[t]}function fn(r){var n,e=new Float64Array(64);for(n=0;64>n;n++)e[n]=r[n];for(n=0;64>n;n++)r[n]=0;an(r,e)}function un(r,n,e,t){var o,i,a=new Uint8Array(64),f=new Uint8Array(64),u=new Uint8Array(64),c=new Float64Array(64),w=[gn(),gn(),gn(),gn()];Q(a,t,32),a[0]&=248,a[31]&=127,a[31]|=64;var y=e+64;for(o=0;e>o;o++)r[64+o]=n[o];for(o=0;32>o;o++)r[32+o]=a[32+o];for(Q(u,r.subarray(32),e+32),fn(u),en(w,u),rn(r,w),o=32;64>o;o++)r[o]=t[o];for(Q(f,r,e+64),fn(f),o=0;64>o;o++)c[o]=0;for(o=0;32>o;o++)c[o]=u[o];for(o=0;32>o;o++)for(i=0;32>i;i++)c[o+i]+=f[o]*a[i];return an(r.subarray(32),c),y}function cn(r,n){var e=gn(),t=gn(),o=gn(),i=gn(),a=gn(),f=gn(),u=gn();return U(r[2],_n),S(r[1],n),C(o,r[1]),Y(i,o,dn),T(o,o,r[2]),K(i,r[2],i),C(a,i),C(f,a),Y(u,f,a),Y(e,u,o),Y(e,e,i),R(e,e),Y(e,e,o),Y(e,e,i),Y(e,e,i),Y(r[0],e,i),C(t,r[0]),Y(t,t,i),m(t,o)&&Y(r[0],r[0],Bn),C(t,r[0]),Y(t,t,i),m(t,o)?-1:(B(r[0])===n[31]>>7&&T(r[0],An,r[0]),Y(r[3],r[0],r[1]),0)}function wn(r,n,e,t){var o,i,a=new Uint8Array(32),f=new Uint8Array(64),c=[gn(),gn(),gn(),gn()],w=[gn(),gn(),gn(),gn()];if(i=-1,64>e)return-1;if(cn(w,t))return-1;for(o=0;e>o;o++)r[o]=n[o];for(o=0;32>o;o++)r[o+32]=t[o];if(Q(f,r,e),fn(f),nn(c,w,f),en(w,n.subarray(32)),W(c,w),rn(a,c),e-=64,u(n,0,a,0)){for(o=0;e>o;o++)r[o]=0;return-1}for(o=0;e>o;o++)r[o]=n[o+64];return i=e}function yn(r,n){if(r.length!==kn)throw new Error("bad key size");if(n.length!==zn)throw new Error("bad nonce size")}function sn(r,n){if(r.length!==Mn)throw new Error("bad public key size");if(n.length!==Gn)throw new Error("bad secret key size")}function ln(){for(var r,n={}.toString,e=0;e<arguments.length;e++)if("[object Uint8Array]"!==(r=n.call(arguments[e])))throw new TypeError("unexpected type "+r+", use Uint8Array")}var hn=function(r,n){this.hi=0|r,this.lo=0|n},gn=function(r){var n,e=new Float64Array(16);if(r)for(n=0;n<r.length;n++)e[n]=r[n];return e},vn=function(){throw new Error("no PRNG")},bn=new Uint8Array(16),pn=new Uint8Array(32);pn[0]=9;var An=gn(),_n=gn([1]),Un=gn([56129,1]),dn=gn([30883,4953,19914,30187,55467,16705,2637,112,59544,30585,16505,36039,65139,11119,27886,20995]),En=gn([61785,9906,39828,60374,45398,33411,5274,224,53552,61171,33010,6542,64743,22239,55772,9222]),xn=gn([54554,36645,11616,51542,42930,38181,51040,26924,56412,64982,57905,49316,21502,52590,14035,8553]),mn=gn([26200,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214]),Bn=gn([41136,18958,6951,50414,58488,44335,6150,12099,55207,15867,153,11085,57099,20417,9344,11139]),Sn=new Uint8Array([101,120,112,97,110,100,32,51,50,45,98,121,116,101,32,107]),Kn=new Uint32Array([5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,252]),Tn=A,Yn=_,Cn=[new hn(1116352408,3609767458),new hn(1899447441,602891725),new hn(3049323471,3964484399),new hn(3921009573,2173295548),new hn(961987163,4081628472),new hn(1508970993,3053834265),new hn(2453635748,2937671579),new hn(2870763221,3664609560),new hn(3624381080,2734883394),new hn(310598401,1164996542),new hn(607225278,1323610764),new hn(1426881987,3590304994),new hn(1925078388,4068182383),new hn(2162078206,991336113),new hn(2614888103,633803317),new hn(3248222580,3479774868),new hn(3835390401,2666613458),new hn(4022224774,944711139),new hn(264347078,2341262773),new hn(604807628,2007800933),new hn(770255983,1495990901),new hn(1249150122,1856431235),new hn(1555081692,3175218132),new hn(1996064986,2198950837),new hn(2554220882,3999719339),new hn(2821834349,766784016),new hn(2952996808,2566594879),new hn(3210313671,3203337956),new hn(3336571891,1034457026),new hn(3584528711,2466948901),new hn(113926993,3758326383),new hn(338241895,168717936),new hn(666307205,1188179964),new hn(773529912,1546045734),new hn(1294757372,1522805485),new hn(1396182291,2643833823),new hn(1695183700,2343527390),new hn(1986661051,1014477480),new hn(2177026350,1206759142),new hn(2456956037,344077627),new hn(2730485921,1290863460),new hn(2820302411,3158454273),new hn(3259730800,3505952657),new hn(3345764771,106217008),new hn(3516065817,3606008344),new hn(3600352804,1432725776),new hn(4094571909,1467031594),new hn(275423344,851169720),new hn(430227734,3100823752),new hn(506948616,1363258195),new hn(659060556,3750685593),new hn(883997877,3785050280),new hn(958139571,3318307427),new hn(1322822218,3812723403),new hn(1537002063,2003034995),new hn(1747873779,3602036899),new hn(1955562222,1575990012),new hn(2024104815,1125592928),new hn(2227730452,2716904306),new hn(2361852424,442776044),new hn(2428436474,593698344),new hn(2756734187,3733110249),new hn(3204031479,2999351573),new hn(3329325298,3815920427),new hn(3391569614,3928383900),new hn(3515267271,566280711),new hn(3940187606,3454069534),new hn(4118630271,4000239992),new hn(116418474,1914138554),new hn(174292421,2731055270),new hn(289380356,3203993006),new hn(460393269,320620315),new hn(685471733,587496836),new hn(852142971,1086792851),new hn(1017036298,365543100),new hn(1126000580,2618297676),new hn(1288033470,3409855158),new hn(1501505948,4234509866),new hn(1607167915,987167468),new hn(1816402316,1246189591)],Ln=new Uint8Array([106,9,230,103,243,188,201,8,187,103,174,133,132,202,167,59,60,110,243,114,254,148,248,43,165,79,245,58,95,29,54,241,81,14,82,127,173,230,130,209,155,5,104,140,43,62,108,31,31,131,217,171,251,65,189,107,91,224,205,25,19,126,33,121]),Rn=new Float64Array([237,211,245,92,26,99,18,88,214,156,247,162,222,249,222,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16]),kn=32,zn=24,Pn=32,Fn=16,Nn=32,On=32,Mn=32,Gn=32,In=32,Zn=zn,jn=Pn,Vn=Fn,qn=64,Xn=32,Dn=32,Hn=64,Jn=64;r.lowlevel={crypto_stream_xor:g,crypto_stream:h,crypto_stream_salsa20_xor:s,crypto_stream_salsa20:l,crypto_onetimeauth:b,crypto_onetimeauth_verify:p,crypto_verify_16:f,crypto_verify_32:u,crypto_secretbox:A,crypto_secretbox_open:_,crypto_scalarmult:k,crypto_scalarmult_base:z,crypto_box_beforenm:F,crypto_box_afternm:Tn,crypto_box:N,crypto_box_open:O,crypto_box_keypair:P,crypto_hash:Q,crypto_hashblocks:J,crypto_sign:un,crypto_sign_keypair:on,crypto_sign_open:wn,crypto_secretbox_KEYBYTES:kn,crypto_secretbox_NONCEBYTES:zn,crypto_secretbox_ZEROBYTES:Pn,crypto_secretbox_BOXZEROBYTES:Fn,crypto_scalarmult_BYTES:Nn,crypto_scalarmult_SCALARBYTES:On,crypto_box_PUBLICKEYBYTES:Mn,crypto_box_SECRETKEYBYTES:Gn,crypto_box_BEFORENMBYTES:In,crypto_box_NONCEBYTES:Zn,crypto_box_ZEROBYTES:jn,crypto_box_BOXZEROBYTES:Vn,crypto_sign_BYTES:qn,crypto_sign_SEEDBYTES:Xn,crypto_sign_PUBLICKEYBYTES:Dn,crypto_sign_SECRETKEYBYTES:Hn,crypto_hash_BYTES:Jn},r.util={},r.util.decodeUTF8=function(r){var n,e=unescape(encodeURIComponent(r)),t=new Uint8Array(e.length);for(n=0;n<e.length;n++)t[n]=e.charCodeAt(n);return t},r.util.encodeUTF8=function(r){var n,e=[];for(n=0;n<r.length;n++)e.push(String.fromCharCode(r[n]));return decodeURIComponent(escape(e.join("")))},r.util.encodeBase64=function(r){if("undefined"==typeof btoa)return new Buffer(r).toString("base64");var n,e=[],t=r.length;for(n=0;t>n;n++)e.push(String.fromCharCode(r[n]));return btoa(e.join(""))},r.util.decodeBase64=function(r){if("undefined"==typeof atob)return new Uint8Array(Array.prototype.slice.call(new Buffer(r,"base64"),0));var n,e=atob(r),t=new Uint8Array(e.length);for(n=0;n<e.length;n++)t[n]=e.charCodeAt(n);return t},r.randomBytes=function(r){var n=new Uint8Array(r);return vn(n,r),n},r.secretbox=function(r,n,e){ln(r,n,e),yn(e,n);for(var t=new Uint8Array(Pn+r.length),o=new Uint8Array(t.length),i=0;i<r.length;i++)t[i+Pn]=r[i];return A(o,t,t.length,n,e),o.subarray(Fn)},r.secretbox.open=function(r,n,e){ln(r,n,e),yn(e,n);for(var t=new Uint8Array(Fn+r.length),o=new Uint8Array(t.length),i=0;i<r.length;i++)t[i+Fn]=r[i];return t.length<32?!1:0!==_(o,t,t.length,n,e)?!1:o.subarray(Pn)},r.secretbox.keyLength=kn,r.secretbox.nonceLength=zn,r.secretbox.overheadLength=Fn,r.scalarMult=function(r,n){if(ln(r,n),r.length!==On)throw new Error("bad n size");if(n.length!==Nn)throw new Error("bad p size");var e=new Uint8Array(Nn);return k(e,r,n),e},r.scalarMult.base=function(r){if(ln(r),r.length!==On)throw new Error("bad n size");var n=new Uint8Array(Nn);return z(n,r),n},r.scalarMult.scalarLength=On,r.scalarMult.groupElementLength=Nn,r.box=function(n,e,t,o){var i=r.box.before(t,o);return r.secretbox(n,e,i)},r.box.before=function(r,n){ln(r,n),sn(r,n);var e=new Uint8Array(In);return F(e,r,n),e},r.box.after=r.secretbox,r.box.open=function(n,e,t,o){var i=r.box.before(t,o);return r.secretbox.open(n,e,i)},r.box.open.after=r.secretbox.open,r.box.keyPair=function(){var r=new Uint8Array(Mn),n=new Uint8Array(Gn);return P(r,n),{publicKey:r,secretKey:n}},r.box.keyPair.fromSecretKey=function(r){if(ln(r),r.length!==Gn)throw new Error("bad secret key size");var n=new Uint8Array(Mn);return z(n,r),{publicKey:n,secretKey:r}},r.box.publicKeyLength=Mn,r.box.secretKeyLength=Gn,r.box.sharedKeyLength=In,r.box.nonceLength=Zn,r.box.overheadLength=r.secretbox.overheadLength,r.sign=function(r,n){if(ln(r,n),n.length!==Hn)throw new Error("bad secret key size");var e=new Uint8Array(qn+r.length);un(e,r,r.length,n);for(var t=new Uint8Array(qn),o=0;o<t.length;o++)t[o]=e[o];return t},r.sign.open=function(r,n,e){if(ln(r,n,e),n.length!==qn)throw new Error("bad signature size");if(e.length!==Dn)throw new Error("bad public key size");var t,o=new Uint8Array(qn+r.length),i=new Uint8Array(qn+r.length);for(t=0;qn>t;t++)o[t]=n[t];for(t=0;t<r.length;t++)o[t+qn]=r[t];var a=wn(i,o,o.length,e);return 0>a?!1:i.subarray(0,a)},r.sign.keyPair=function(){var r=new Uint8Array(Dn),n=new Uint8Array(Hn);return on(r,n),{publicKey:r,secretKey:n}},r.sign.keyPair.fromSecretKey=function(r){if(ln(r),r.length!==Hn)throw new Error("bad secret key size");var n,e=new Uint8Array(Dn);for(n=0;32>n;n++)e[n]=r[32+n];return{publicKey:e,secretKey:r}},r.sign.keyPair.fromSeed=function(r){if(ln(r),r.length!==Xn)throw new Error("bad seed size");var n=new Uint8Array(Dn),e=new Uint8Array(Hn);return tn(r,n,e),{publicKey:n,secretKey:e}},r.sign.publicKeyLength=Dn,r.sign.secretKeyLength=Hn,r.sign.signatureLength=qn,r.hash=function(r){ln(r);var n=new Uint8Array(Jn);return Q(n,r,r.length),n},r.hash.hashLength=Jn,r.verify=function(r,n){return ln(r,n),0===r.length||0===n.length?!1:r.length!==n.length?!1:0===a(r,0,n,0,r.length)?!0:!1},r.setPRNG=function(r){vn=r},function(){var n;"undefined"!=typeof window?(window.crypto&&window.crypto.getRandomValues?n=window.crypto:window.msCrypto&&window.msCrypto.getRandomValues&&(n=window.msCrypto),n&&r.setPRNG(function(r,e){var t,o=new Uint8Array(e);for(n.getRandomValues(o),t=0;e>t;t++)r[t]=o[t]})):"undefined"!=typeof require&&(n=require("crypto"),n&&r.setPRNG(function(r,e){var t,o=n.randomBytes(e);for(t=0;e>t;t++)r[t]=o[t]}))}()}("undefined"!=typeof module&&module.exports?module.exports:window.nacl=window.nacl||{});