Advertisement
zanda268

Startup

Aug 18th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.23 KB | None | 0 0
  1. local function e(n)
  2. local s=setmetatable({},{__index=_ENV or getfenv()})if setfenv then setfenv(n,s)end;return n(s)or s end
  3. local t=e(function(n,...)local s=math.floor;local h,r
  4. r=function(d,l)return s(d%4294967296/2^l)end
  5. h=function(d,l)return(d*2^l)%4294967296 end
  6. return{bnot=bit.bnot,band=bit.band,bor=bit.bor,bxor=bit.bxor,rshift=r,lshift=h}end)
  7. local a=e(function(n,...)local s=t.bxor;local h=t.lshift;local r=0x100;local d=0xff;local l=0x11b;local u={}local c={}
  8. local function m(k,q)return s(k,q)end;local function f(k,q)return s(k,q)end
  9. local function w(k)if(k==1)then return 1 end;local q=d-c[k]return u[q]end;local function y(k,q)if(k==0 or q==0)then return 0 end;local j=c[k]+c[q]
  10. if(j>=d)then j=j-d end;return u[j]end
  11. local function p(k,q)
  12. if(k==0)then return 0 end;local j=c[k]-c[q]if(j<0)then j=j+d end;return u[j]end
  13. local function v()for k=1,r do print("log(",k-1,")=",c[k-1])end end
  14. local function b()for k=1,r do print("exp(",k-1,")=",u[k-1])end end;local function g()local k=1
  15. for q=0,d-1 do u[q]=k;c[k]=q;k=s(h(k,1),k)if k>d then k=f(k,l)end end end;g()return
  16. {add=m,sub=f,invert=w,mul=y,div=dib,printLog=v,printExp=b}end)
  17. util=e(function(n,...)local s=t.bxor;local h=t.rshift;local r=t.band;local d=t.lshift;local l;local function u(O)O=s(O,h(O,4))
  18. O=s(O,h(O,2))O=s(O,h(O,1))return r(O,1)end
  19. local function c(O,I)if(I==0)then return
  20. r(O,0xff)else return r(h(O,I*8),0xff)end end;local function m(O,I)
  21. if(I==0)then return r(O,0xff)else return d(r(O,0xff),I*8)end end
  22. local function f(O,I,N)local S={}
  23. for H=0,N-1 do
  24. S[H+1]=
  25. m(O[I+ (H*4)],3)+m(O[I+ (H*4)+1],2)+
  26. m(O[I+ (H*4)+2],1)+m(O[I+ (H*4)+3],0)if N%10000 ==0 then l()end end;return S end;local function w(O,I,N,S)S=S or#O;for H=0,S-1 do
  27. for R=0,3 do I[N+H*4+ (3-R)]=c(O[H+1],R)end;if S%10000 ==0 then l()end end
  28. return I end;local function y(O)local I=""
  29. for N,S in
  30. ipairs(O)do I=I..string.format("%02x ",S)end;return I end
  31. local function p(O)local I={}for N=1,#O,2 do I[#I+1]=tonumber(O:sub(N,
  32. N+1),16)end;return I end
  33. local function v(O)local I=type(O)
  34. if(I=="number")then return string.format("%08x",O)elseif
  35. (I=="table")then return y(O)elseif(I=="string")then local N={string.byte(O,1,#O)}
  36. return y(N)else return O end end
  37. local function b(O)local I=#O;local N=math.random(0,255)local S=math.random(0,255)
  38. local H=string.char(N,S,N,S,c(I,3),c(I,2),c(I,1),c(I,0))O=H..O;local R=math.ceil(#O/16)*16-#O;local D=""for L=1,R do D=D..
  39. string.char(math.random(0,255))end;return O..D end
  40. local function g(O)local I={string.byte(O,1,4)}if
  41. (I[1]==I[3]and I[2]==I[4])then return true end;return false end
  42. local function k(O)if(not g(O))then return nil end
  43. local I=
  44. m(string.byte(O,5),3)+
  45. m(string.byte(O,6),2)+m(string.byte(O,7),1)+m(string.byte(O,8),0)return string.sub(O,9,8+I)end;local function q(O,I)for N=1,16 do O[N]=s(O[N],I[N])end end
  46. local function j(O)
  47. local I=16;while true do local N=O[I]+1
  48. if N>=256 then O[I]=N-256;I=(I-2)%16+1 else O[I]=N;break end end end;local x,z,_=os.queueEvent,coroutine.yield,os.time;local E=_()
  49. local function l()local O=_()if O-E>=0.03 then E=O
  50. x("sleep")z("sleep")end end
  51. local function T(O)local I,N,S,H=string.char,math.random,l,table.insert;local R={}for D=1,O do
  52. H(R,N(0,255))if D%10240 ==0 then S()end end;return R end
  53. local function A(O)local I,N,S,H=string.char,math.random,l,table.insert;local R={}for D=1,O do
  54. H(R,I(N(0,255)))if D%10240 ==0 then S()end end
  55. return table.concat(R)end
  56. return
  57. {byteParity=u,getByte=c,putByte=m,bytesToInts=f,intsToBytes=w,bytesToHex=y,hexToBytes=p,toHexString=v,padByteString=b,properlyDecrypted=g,unpadByteString=k,xorIV=q,increment=j,sleepCheckIn=l,getRandomData=T,getRandomString=A}end)
  58. aes=e(function(n,...)local s=util.putByte;local h=util.getByte;local r='rounds'local d="type"local l=1;local u=2
  59. local c={}local m={}local f={}local w={}local y={}local p={}local v={}local b={}local g={}local k={}
  60. local q={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,0xab000000,0x4d000000,0x9a000000,0x2f000000}
  61. local function j(M)mask=0xf8;result=0
  62. for F=1,8 do result=t.lshift(result,1)
  63. parity=util.byteParity(t.band(M,mask))result=result+parity;lastbit=t.band(mask,1)
  64. mask=t.band(t.rshift(mask,1),0xff)
  65. if(lastbit~=0)then mask=t.bor(mask,0x80)else mask=t.band(mask,0x7f)end end;return t.bxor(result,0x63)end;local function x()
  66. for M=0,255 do if(M~=0)then inverse=a.invert(M)else inverse=M end
  67. mapped=j(inverse)c[M]=mapped;m[mapped]=M end end
  68. local function z()
  69. for M=0,255 do
  70. byte=c[M]
  71. f[M]=
  72. s(a.mul(0x03,byte),0)+s(byte,1)+s(byte,2)+s(a.mul(0x02,byte),3)w[M]=s(byte,0)+s(byte,1)+s(a.mul(0x02,byte),2)+
  73. s(a.mul(0x03,byte),3)y[M]=
  74.  
  75. s(byte,0)+s(a.mul(0x02,byte),1)+s(a.mul(0x03,byte),2)+s(byte,3)p[M]=
  76.  
  77. s(a.mul(0x02,byte),0)+s(a.mul(0x03,byte),1)+s(byte,2)+s(byte,3)end end
  78. local function _()
  79. for M=0,255 do byte=m[M]
  80. v[M]=
  81. s(a.mul(0x0b,byte),0)+s(a.mul(0x0d,byte),1)+s(a.mul(0x09,byte),2)+
  82. s(a.mul(0x0e,byte),3)
  83. b[M]=
  84. s(a.mul(0x0d,byte),0)+s(a.mul(0x09,byte),1)+s(a.mul(0x0e,byte),2)+
  85. s(a.mul(0x0b,byte),3)
  86. g[M]=
  87. s(a.mul(0x09,byte),0)+s(a.mul(0x0e,byte),1)+s(a.mul(0x0b,byte),2)+
  88. s(a.mul(0x0d,byte),3)
  89. k[M]=
  90. s(a.mul(0x0e,byte),0)+s(a.mul(0x0b,byte),1)+s(a.mul(0x0d,byte),2)+
  91. s(a.mul(0x09,byte),3)end end;local function E(M)local F=t.band(M,0xff000000)return
  92. (t.lshift(M,8)+t.rshift(F,24))end
  93. local function T(M)return
  94. s(c[h(M,0)],0)+s(c[h(M,1)],1)+s(c[h(M,2)],2)+
  95. s(c[h(M,3)],3)end
  96. local function A(M)local F={}local W=math.floor(#M/4)if(
  97. (W~=4 and W~=6 and W~=8)or(W*4 ~=#M))then
  98. error("Invalid key size: "..tostring(W))return nil end;F[r]=W+6;F[d]=l;for Y=0,W-1
  99. do
  100. F[Y]=
  101. s(M[Y*4+1],3)+s(M[Y*4+2],2)+s(M[Y*4+3],1)+s(M[Y*4+4],0)end
  102. for Y=W,(F[r]+1)*4-1 do
  103. local P=F[Y-1]
  104. if(Y%W==0)then P=E(P)P=T(P)local V=math.floor(Y/W)P=t.bxor(P,q[V])elseif(
  105. W>6 and Y%W==4)then P=T(P)end;F[Y]=t.bxor(F[(Y-W)],P)end;return F end
  106. local function O(M)local F=h(M,3)local W=h(M,2)local Y=h(M,1)local P=h(M,0)
  107. return
  108.  
  109.  
  110.  
  111. s(a.add(a.add(a.add(a.mul(0x0b,W),a.mul(0x0d,Y)),a.mul(0x09,P)),a.mul(0x0e,F)),3)+
  112. s(a.add(a.add(a.add(a.mul(0x0b,Y),a.mul(0x0d,P)),a.mul(0x09,F)),a.mul(0x0e,W)),2)+
  113. s(a.add(a.add(a.add(a.mul(0x0b,P),a.mul(0x0d,F)),a.mul(0x09,W)),a.mul(0x0e,Y)),1)+
  114. s(a.add(a.add(a.add(a.mul(0x0b,F),a.mul(0x0d,W)),a.mul(0x09,Y)),a.mul(0x0e,P)),0)end
  115. local function I(M)local F=h(M,3)local W=h(M,2)local Y=h(M,1)local P=h(M,0)local V=t.bxor(P,Y)
  116. local B=t.bxor(W,F)local G=t.bxor(V,B)G=t.bxor(G,a.mul(0x08,G))
  117. w=t.bxor(G,a.mul(0x04,t.bxor(Y,F)))G=t.bxor(G,a.mul(0x04,t.bxor(P,W)))
  118. return
  119.  
  120.  
  121. s(t.bxor(t.bxor(P,G),a.mul(0x02,t.bxor(F,P))),0)+s(t.bxor(t.bxor(Y,w),a.mul(0x02,V)),1)+
  122. s(t.bxor(t.bxor(W,G),a.mul(0x02,t.bxor(F,P))),2)+
  123. s(t.bxor(t.bxor(F,w),a.mul(0x02,B)),3)end
  124. local function N(M)local F=A(M)if(F==nil)then return nil end;F[d]=u;for W=4,(F[r]+1)*4-5 do
  125. F[W]=O(F[W])end;return F end;local function S(M,F,W)
  126. for Y=0,3 do M[Y+1]=t.bxor(M[Y+1],F[W*4+Y])end end
  127. local function H(M,F)
  128. F[1]=t.bxor(t.bxor(t.bxor(f[h(M[1],3)],w[h(M[2],2)]),y[h(M[3],1)]),p[h(M[4],0)])
  129. F[2]=t.bxor(t.bxor(t.bxor(f[h(M[2],3)],w[h(M[3],2)]),y[h(M[4],1)]),p[h(M[1],0)])
  130. F[3]=t.bxor(t.bxor(t.bxor(f[h(M[3],3)],w[h(M[4],2)]),y[h(M[1],1)]),p[h(M[2],0)])
  131. F[4]=t.bxor(t.bxor(t.bxor(f[h(M[4],3)],w[h(M[1],2)]),y[h(M[2],1)]),p[h(M[3],0)])end
  132. local function R(M,F)
  133. F[1]=s(c[h(M[1],3)],3)+s(c[h(M[2],2)],2)+
  134. s(c[h(M[3],1)],1)+s(c[h(M[4],0)],0)
  135. F[2]=s(c[h(M[2],3)],3)+s(c[h(M[3],2)],2)+
  136. s(c[h(M[4],1)],1)+s(c[h(M[1],0)],0)
  137. F[3]=s(c[h(M[3],3)],3)+s(c[h(M[4],2)],2)+
  138. s(c[h(M[1],1)],1)+s(c[h(M[2],0)],0)
  139. F[4]=s(c[h(M[4],3)],3)+s(c[h(M[1],2)],2)+
  140. s(c[h(M[2],1)],1)+s(c[h(M[3],0)],0)end
  141. local function D(M,F)
  142. F[1]=t.bxor(t.bxor(t.bxor(v[h(M[1],3)],b[h(M[4],2)]),g[h(M[3],1)]),k[h(M[2],0)])
  143. F[2]=t.bxor(t.bxor(t.bxor(v[h(M[2],3)],b[h(M[1],2)]),g[h(M[4],1)]),k[h(M[3],0)])
  144. F[3]=t.bxor(t.bxor(t.bxor(v[h(M[3],3)],b[h(M[2],2)]),g[h(M[1],1)]),k[h(M[4],0)])
  145. F[4]=t.bxor(t.bxor(t.bxor(v[h(M[4],3)],b[h(M[3],2)]),g[h(M[2],1)]),k[h(M[1],0)])end
  146. local function L(M,F)
  147. F[1]=s(m[h(M[1],3)],3)+s(m[h(M[4],2)],2)+
  148. s(m[h(M[3],1)],1)+s(m[h(M[2],0)],0)
  149. F[2]=s(m[h(M[2],3)],3)+s(m[h(M[1],2)],2)+
  150. s(m[h(M[4],1)],1)+s(m[h(M[3],0)],0)
  151. F[3]=s(m[h(M[3],3)],3)+s(m[h(M[2],2)],2)+
  152. s(m[h(M[1],1)],1)+s(m[h(M[4],0)],0)
  153. F[4]=s(m[h(M[4],3)],3)+s(m[h(M[3],2)],2)+
  154. s(m[h(M[2],1)],1)+s(m[h(M[1],0)],0)end
  155. local function U(M,F,W,Y,P)W=W or 1;Y=Y or{}P=P or 1;local V={}local B={}if(M[d]~=l)then
  156. error("No encryption key: "..
  157. tostring(M[d])..", expected "..l)return end
  158. V=util.bytesToInts(F,W,4)S(V,M,0)local G=1;while(G<M[r]-1)do H(V,B)S(B,M,G)G=G+1;H(B,V)S(V,M,G)
  159. G=G+1 end;H(V,B)S(B,M,G)G=G+1;R(B,V)S(V,M,G)
  160. util.sleepCheckIn()return util.intsToBytes(V,Y,P)end
  161. local function C(M,F,W,Y,P)W=W or 1;Y=Y or{}P=P or 1;local V={}local B={}
  162. if(M[d]~=u)then error("No decryption key: "..
  163. tostring(M[d]))return end;V=util.bytesToInts(F,W,4)S(V,M,M[r])local G=M[r]-1;while(G>2)do
  164. D(V,B)S(B,M,G)G=G-1;D(B,V)S(V,M,G)G=G-1 end;D(V,B)
  165. S(B,M,G)G=G-1;L(B,V)S(V,M,G)util.sleepCheckIn()return
  166. util.intsToBytes(V,Y,P)end;x()z()_()
  167. return{ROUNDS=r,KEY_TYPE=d,ENCRYPTION_KEY=l,DECRYPTION_KEY=u,expandEncryptionKey=A,expandDecryptionKey=N,encrypt=U,decrypt=C}end)
  168. local o=e(function(n,...)local function s()return{}end;local function h(d,l)table.insert(d,l)end;local function r(d)return
  169. table.concat(d)end;return{new=s,addString=h,toString=r}end)
  170. ciphermode=e(function(n,...)local s={}local h=math.random
  171. function s.encryptString(r,d,l,u)if u then local f={}for w=1,16 do f[w]=u[w]end;u=f else
  172. u={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}end
  173. local c=aes.expandEncryptionKey(r)local m=o.new()for f=1,#d/16 do local w=(f-1)*16+1
  174. local y={string.byte(d,w,w+15)}u=l(c,y,u)
  175. o.addString(m,string.char(unpack(y)))end
  176. return o.toString(m)end;function s.encryptECB(r,d,l)aes.encrypt(r,d,1,d,1)end;function s.encryptCBC(r,d,l)
  177. util.xorIV(d,l)aes.encrypt(r,d,1,d,1)return d end;function s.encryptOFB(r,d,l)
  178. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return l end;function s.encryptCFB(r,d,l)
  179. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return d end
  180. function s.encryptCTR(r,d,l)
  181. local u={}for c=1,16 do u[c]=l[c]end;aes.encrypt(r,l,1,l,1)
  182. util.xorIV(d,l)util.increment(u)return u end
  183. function s.decryptString(r,d,l,u)if u then local f={}for w=1,16 do f[w]=u[w]end;u=f else
  184. u={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}end;local c
  185. if l==s.decryptOFB or
  186. l==s.decryptCFB or l==s.decryptCTR then
  187. c=aes.expandEncryptionKey(r)else c=aes.expandDecryptionKey(r)end;local m=o.new()for f=1,#d/16 do local w=(f-1)*16+1
  188. local y={string.byte(d,w,w+15)}u=l(c,y,u)
  189. o.addString(m,string.char(unpack(y)))end
  190. return o.toString(m)end;function s.decryptECB(r,d,l)aes.decrypt(r,d,1,d,1)return l end;function s.decryptCBC(r,d,l)
  191. local u={}for c=1,16 do u[c]=d[c]end;aes.decrypt(r,d,1,d,1)
  192. util.xorIV(d,l)return u end;function s.decryptOFB(r,d,l)
  193. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return l end;function s.decryptCFB(r,d,l)
  194. local u={}for c=1,16 do u[c]=d[c]end;aes.encrypt(r,l,1,l,1)
  195. util.xorIV(d,l)return u end
  196. s.decryptCTR=s.encryptCTR;return s end)AES128=16;AES192=24;AES256=32;ECBMODE=1;CBCMODE=2;OFBMODE=3;CFBMODE=4;CTRMODE=4
  197. local function i(n,s,h)local r=s;if(s==
  198. AES192)then r=32 end
  199. if(r>#n)then local l=""for u=1,r-#n do
  200. l=l..string.char(0)end;n=n..l else n=string.sub(n,1,r)end;local d={string.byte(n,1,#n)}
  201. n=ciphermode.encryptString(d,n,ciphermode.encryptCBC,h)n=string.sub(n,1,s)return{string.byte(n,1,#n)}end
  202. function encrypt(n,s,h,r,d)assert(n~=nil,"Empty password.")
  203. assert(n~=nil,"Empty data.")local r=r or CBCMODE;local h=h or AES128;local l=i(n,h,d)
  204. local u=util.padByteString(s)
  205. if r==ECBMODE then
  206. return ciphermode.encryptString(l,u,ciphermode.encryptECB,d)elseif r==CBCMODE then
  207. return ciphermode.encryptString(l,u,ciphermode.encryptCBC,d)elseif r==OFBMODE then
  208. return ciphermode.encryptString(l,u,ciphermode.encryptOFB,d)elseif r==CFBMODE then
  209. return ciphermode.encryptString(l,u,ciphermode.encryptCFB,d)elseif r==CTRMODE then
  210. return ciphermode.encryptString(l,u,ciphermode.encryptCTR,d)else error("Unknown mode",2)end end
  211. function decrypt(n,s,h,r,d)local r=r or CBCMODE;local h=h or AES128;local l=i(n,h,d)local u
  212. if r==ECBMODE then
  213. u=ciphermode.decryptString(l,s,ciphermode.decryptECB,d)elseif r==CBCMODE then
  214. u=ciphermode.decryptString(l,s,ciphermode.decryptCBC,d)elseif r==OFBMODE then
  215. u=ciphermode.decryptString(l,s,ciphermode.decryptOFB,d)elseif r==CFBMODE then
  216. u=ciphermode.decryptString(l,s,ciphermode.decryptCFB,d)elseif r==CTRMODE then
  217. u=ciphermode.decryptString(l,s,ciphermode.decryptCTR,d)else error("Unknown mode",2)end;result=util.unpadByteString(u)
  218. if(result==nil)then return nil end;return result end
  219.  
  220.  
  221. --  SHA 256
  222. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  223. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  224. --  
  225. --  Using an adapted version of the bit library
  226. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  227. --  
  228. local MOD = 2^32
  229. local MODM = MOD-1
  230.  
  231. local function memoize(f)
  232.     local mt = {}
  233.     local t = setmetatable({}, mt)
  234.     function mt:__index(k)
  235.         local v = f(k)
  236.         t[k] = v
  237.         return v
  238.     end
  239.     return t
  240. end
  241.  
  242. local function make_bitop_uncached(t, m)
  243.     local function bitop(a, b)
  244.         local res,p = 0,1
  245.         while a ~= 0 and b ~= 0 do
  246.             local am, bm = a % m, b % m
  247.             res = res + t[am][bm] * p
  248.             a = (a - am) / m
  249.             b = (b - bm) / m
  250.             p = p*m
  251.         end
  252.         res = res + (a + b) * p
  253.         return res
  254.     end
  255.     return bitop
  256. end
  257.  
  258. local function make_bitop(t)
  259.     local op1 = make_bitop_uncached(t,2^1)
  260.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  261.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  262. end
  263.  
  264. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  265.  
  266. local function bxor(a, b, c, ...)
  267.     local z = nil
  268.     if b then
  269.         a = a % MOD
  270.         b = b % MOD
  271.         z = bxor1(a, b)
  272.         if c then z = bxor(z, c, ...) end
  273.         return z
  274.     elseif a then return a % MOD
  275.     else return 0 end
  276. end
  277.  
  278. local function band(a, b, c, ...)
  279.     local z
  280.     if b then
  281.         a = a % MOD
  282.         b = b % MOD
  283.         z = ((a + b) - bxor1(a,b)) / 2
  284.         if c then z = bit32_band(z, c, ...) end
  285.         return z
  286.     elseif a then return a % MOD
  287.     else return MODM end
  288. end
  289.  
  290. local function bnot(x) return (-1 - x) % MOD end
  291.  
  292. local function rshift1(a, disp)
  293.     if disp < 0 then return lshift(a,-disp) end
  294.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  295. end
  296.  
  297. local function rshift(x, disp)
  298.     if disp > 31 or disp < -31 then return 0 end
  299.     return rshift1(x % MOD, disp)
  300. end
  301.  
  302. local function lshift(a, disp)
  303.     if disp < 0 then return rshift(a,-disp) end
  304.     return (a * 2 ^ disp) % 2 ^ 32
  305. end
  306.  
  307. local function rrotate(x, disp)
  308.     x = x % MOD
  309.     disp = disp % 32
  310.     local low = band(x, 2 ^ disp - 1)
  311.     return rshift(x, disp) + lshift(low, 32 - disp)
  312. end
  313.  
  314. local k = {
  315.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  316.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  317.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  318.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  319.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  320.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  321.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  322.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  323.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  324.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  325.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  326.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  327.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  328.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  329.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  330.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  331. }
  332.  
  333. local function str2hexa(s)
  334.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  335. end
  336.  
  337. local function num2s(l, n)
  338.     local s = ""
  339.     for i = 1, n do
  340.         local rem = l % 256
  341.         s = string.char(rem) .. s
  342.         l = (l - rem) / 256
  343.     end
  344.     return s
  345. end
  346.  
  347. local function s232num(s, i)
  348.     local n = 0
  349.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  350.     return n
  351. end
  352.  
  353. local function preproc(msg, len)
  354.     local extra = 64 - ((len + 9) % 64)
  355.     len = num2s(8 * len, 8)
  356.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  357.     assert(#msg % 64 == 0)
  358.     return msg
  359. end
  360.  
  361. local function initH256(H)
  362.     H[1] = 0x6a09e667
  363.     H[2] = 0xbb67ae85
  364.     H[3] = 0x3c6ef372
  365.     H[4] = 0xa54ff53a
  366.     H[5] = 0x510e527f
  367.     H[6] = 0x9b05688c
  368.     H[7] = 0x1f83d9ab
  369.     H[8] = 0x5be0cd19
  370.     return H
  371. end
  372.  
  373. local function digestblock(msg, i, H)
  374.     local w = {}
  375.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  376.     for j = 17, 64 do
  377.         local v = w[j - 15]
  378.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  379.         v = w[j - 2]
  380.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  381.     end
  382.  
  383.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  384.     for i = 1, 64 do
  385.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  386.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  387.         local t2 = s0 + maj
  388.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  389.         local ch = bxor (band(e, f), band(bnot(e), g))
  390.         local t1 = h + s1 + ch + k[i] + w[i]
  391.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  392.     end
  393.  
  394.     H[1] = band(H[1] + a)
  395.     H[2] = band(H[2] + b)
  396.     H[3] = band(H[3] + c)
  397.     H[4] = band(H[4] + d)
  398.     H[5] = band(H[5] + e)
  399.     H[6] = band(H[6] + f)
  400.     H[7] = band(H[7] + g)
  401.     H[8] = band(H[8] + h)
  402. end
  403.  
  404. local function sha256(msg)
  405.     msg = preproc(msg, #msg)
  406.     local H = initH256({})
  407.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  408.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  409.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  410. end
  411.  
  412.  
  413. local function hashVal(pwd, salt, count)
  414.     local hash = pwd .. salt
  415.    
  416.     for i=1, count do
  417.         hash = sha256(hash)
  418.     end
  419.     return hash
  420. end
  421.  
  422. local function isAuthValid(auth)
  423.     local request = http.post("http://zmeyer.com/auth.php", "auth="..textutils.urlEncode(tostring(auth)))
  424.    
  425.     return request.readLine() == "true"
  426. end
  427.  
  428. local function checkAuth()
  429.     auth = ""
  430.  
  431.     if not fs.exists("auth") then
  432.         print("Please enter an authorization code.")
  433.         write(">")
  434.         auth = read()
  435.  
  436.         if isAuthValid(auth) then
  437.             local f = fs.open("auth","w")
  438.             f.writeLine(auth)
  439.             f.close()
  440.             return true
  441.         else
  442.             return false
  443.         end
  444.  
  445.     else
  446.         local f = fs.open("auth", "r")
  447.         auth = f.readLine()
  448.         f.close()
  449.  
  450.         if isAuthValid(auth)then
  451.             return true
  452.         else
  453.             fs.delete("auth")
  454.             return false
  455.         end
  456.     end
  457. end
  458.  
  459.  
  460.  
  461. local PWD_SALT_HASH_COUNT = 4
  462. local PWD_HASH_COUNT = 29
  463. local KEY_HASH_COUNT = 19
  464. local PASSWORD_FILE = "/.pwd.pwd"
  465. local PASSWORD_FILE_NAME = ".pwd.pwd"
  466.  
  467. local function newSalt()
  468.     return hashVal("", os.time() .. os.day(), PWD_SALT_HASH_COUNT)
  469. end
  470.  
  471. local key = ""
  472.  
  473. local oldFs = {}
  474.  
  475. for k, v in pairs(fs) do
  476.     oldFs[k] = v
  477. end
  478.  
  479. local function checkPath(path)
  480.     return not path:find(PASSWORD_FILE_NAME)
  481. end
  482.  
  483. local function shouldEncrypt(file)
  484.     file = oldFs.combine("", file)
  485.     return not oldFs.isReadOnly(file)
  486.     and (oldFs.getDrive(file) == "hdd" or (oldFs.getDrive(file) == nil and file:sub(1, 4) ~= "rom/" and (not file:find("^disk%d*/"))))
  487.     and (oldFs.getName(file) ~= "startup" or oldFs.getDir(file) ~= "")
  488.     and file ~= PASSWORD_FILE_NAME
  489.     and file ~= PASSWORD_FILE
  490. end
  491.  
  492. local function encryptFile(file)
  493.     if fs.isDir(file) then
  494.         for k, v in ipairs(fs.list(file)) do
  495.             if shouldEncrypt(file .. "/" .. v) then
  496.                 encryptFile(file .. "/" .. v)
  497.             end
  498.         end
  499.     else
  500.         local f = oldFs.open(file, "r")
  501.         local encrypted = encrypt(key, f.readAll())
  502.         f.close()
  503.         f = oldFs.open(file, "w")
  504.         f.write(encrypted)
  505.         f.close()
  506.     end
  507. end
  508.  
  509. local oldPullEvent = os.pullEvent
  510.  
  511. os.pullEvent = os.pullEventRaw
  512.  
  513. while not checkAuth() do end
  514.  
  515. if fs.exists(PASSWORD_FILE) then
  516.     local f = fs.open(PASSWORD_FILE, "r")
  517.     local hash = f.readLine()
  518.     local salt = f.readLine()
  519.     f.close()
  520.     print("Enter password")
  521.     write(">")
  522.     local input = read("*")
  523.     while hashVal(input, salt, PWD_HASH_COUNT) ~= hash do
  524.         print("Incorrect password")
  525.         write(">")
  526.         input = read("*")
  527.     end
  528.     key = hashVal(input, salt, KEY_HASH_COUNT)
  529. else
  530.     print("Enter a new password")
  531.     write(">")
  532.     local input = read("*")
  533.     while input:len() < 8 do
  534.         print("Password must be 8 characters or more")
  535.         write(">")
  536.         input = read("*")
  537.     end
  538.     local salt = newSalt()
  539.     local hash = hashVal(input, salt, PWD_HASH_COUNT)
  540.     math.randomseed(os.day())
  541.     local f = fs.open(PASSWORD_FILE, "w")
  542.     f.writeLine(hash)
  543.     f.writeLine(salt)
  544.     f.close()
  545.    
  546.     key = hashVal(input, salt, KEY_HASH_COUNT)
  547.     encryptFile("")
  548.    
  549. end
  550.  
  551. function fs.list(path)
  552.     local t = oldFs.list(path)
  553.     local t2 = {}
  554.     for k, v in ipairs(t) do
  555.         if checkPath(v) then
  556.             table.insert(t2, v)
  557.         end
  558.     end
  559.     return t2
  560. end
  561.  
  562. function fs.exists(path)
  563.     return checkPath(path) and oldFs.exists(path)
  564. end
  565.  
  566. function fs.getDrive(path)
  567.     if not checkPath(path) then
  568.         return nil
  569.     else
  570.         return oldFs.getDrive(path)
  571.     end
  572. end
  573.  
  574. function fs.getSize(path)
  575.     if not checkpath(path) then
  576.         error("Access denied")
  577.     else
  578.         return oldFs.getSize(path)
  579.     end
  580. end
  581.  
  582. function fs.move(fromPath, toPath)
  583.     if checkPath(fromPath) and checkPath(toPath) then
  584.         oldFs.move(fromPath, toPath)
  585.     else
  586.         error("Access denied")
  587.     end
  588. end
  589.  
  590. function fs.copy(fromPath, toPath)
  591.     if checkPath(fromPath) and checkPath(toPath) then
  592.         oldFs.copy(fromPath, toPath)
  593.     else
  594.         error("Access denied")
  595.     end
  596. end
  597.  
  598. function fs.delete(path)
  599.     if checkPath(path)then
  600.         oldFs.delete(path)
  601.     else
  602.         error("Access denied")
  603.     end
  604. end
  605.  
  606. if oldFs.find then
  607.     function fs.find(path)
  608.         local t = oldFs.find(path)
  609.         local t2 = {}
  610.         for k, v in ipairs(t) do
  611.             if checkPath(v) then
  612.                 table.insert(t2, v)
  613.             end
  614.         end
  615.         return t2
  616.     end
  617. end
  618.  
  619. if oldFs.complete then
  620.     function fs.complete(partial, path, includeFiles, includeSlashes)
  621.         local t = oldFs.complete(partial, path, includeFiles, includeSlashes)
  622.         local t2 = {}
  623.         for k, v in ipairs(t) do
  624.             if checkPath(v) then
  625.                 table.insert(t2, v)
  626.             end
  627.         end
  628.         return t2
  629.     end
  630. end
  631.  
  632. local function openr(path)
  633.     local f = oldFs.open(path, "r")
  634.     local idx = 1
  635.     local decrypted = decrypt(key, f.readAll())
  636.     -- local decrypted = f.readAll()
  637.    
  638.     local handle = {}
  639.     function handle.close()
  640.         f.close()
  641.     end
  642.     function handle.readLine()
  643.         local i = decrypted:find("\n", idx) or #decrypted + 1
  644.         if idx > #decrypted then
  645.             return nil
  646.         end
  647.         local s = decrypted:sub(idx, i - 1)
  648.         idx = i + 1
  649.         return s
  650.     end
  651.     function handle.readAll()
  652.         if idx > #decrypted then
  653.             return nil
  654.         end
  655.         local s = decrypted:sub(idx)
  656.         if s:sub(-1) == "\n" then
  657.             return s:sub(1, -2)
  658.         end
  659.         return s
  660.     end
  661.     return handle
  662. end
  663.  
  664. local function openw(path)
  665.     local f = oldFs.open(path, "w")
  666.     local decrypted = ""
  667.    
  668.     local handle = {}
  669.     function handle.close()
  670.         f.write(encrypt(key, decrypted))
  671.         -- f.write(decrypted)
  672.         f.close()
  673.     end
  674.     function handle.flush()
  675.         f.close()
  676.         f.open(path, "w")
  677.         f.write(encrypt(key))
  678.         -- f.write(decrypted)  
  679.     end
  680.     function handle.writeLine(val)
  681.         decrypted = decrypted .. val .. "\n"
  682.     end
  683.     function handle.write(val)
  684.         decrypted = decrypted .. val
  685.     end
  686.     return handle
  687. end
  688.  
  689. local function opena(path)
  690.     local f = oldFs.open(path, "r")
  691.     local decrypted = decrypt(key, f.readAll())
  692.     -- local decrypted = f.readAll()
  693.     f.close()
  694.     f = oldFs.open(path, "r")
  695.    
  696.     local handle = {}
  697.     function handle.close()
  698.         f.write(encrypt(key, decrypted))
  699.         -- f.write(decrypted)
  700.         f.close()
  701.     end
  702.     function handle.flush()
  703.         f.close()
  704.         f.open(path, "w")
  705.         f.write(encrypt(key, decrypted))
  706.         -- f.write(decrypted)      
  707.     end
  708.     function handle.writeLine(val)
  709.         decrypted = decrypted .. val .. "\n"
  710.     end
  711.     function handle.write(val)
  712.         decrypted = decrypted .. val
  713.     end
  714.     return handle
  715. end
  716.  
  717. local function openrb(path)
  718.     local f = oldFs.open(path, "r")
  719.     local idx = 1
  720.     local decrypted = decrypt(key, f.readAll())
  721.     -- local decrypted = f.readAll()
  722.    
  723.     local handle = {}
  724.     function handle.close()
  725.         f.close()
  726.     end
  727.     function handle.read()
  728.         idx = idx + 1
  729.         return string.byte(decrypted:sub(idx - 1, idx - 1))
  730.     end
  731.     return handle
  732. end
  733.  
  734. local function openwb(path)
  735.     local f = oldFs.open(path, "w")
  736.     local decrypted = ""
  737.    
  738.     local handle = {}
  739.     function handle.close()
  740.         f.write(encrypt(key, decrypted))
  741.         -- f.write(decrypted)
  742.         f.close()
  743.     end
  744.     function handle.flush()
  745.         f.close()
  746.         f.open(path, "w")
  747.         f.write(encrypt(key, decrypted))
  748.         -- f.write(decrypted)
  749.     end
  750.     function handle.write(val)
  751.         decrypted = decrypted .. string.char(val)
  752.     end
  753.     return handle
  754. end
  755.  
  756. local function openab(path)
  757.     local f = oldFs.open(path, "r")
  758.     local decrypted = decrypt(key, f.readAll())
  759.     -- local decrypted = f.readAll()
  760.     f.close()
  761.     f = oldFs.open(path, "r")
  762.    
  763.     local handle = {}
  764.     function handle.close()
  765.         f.write(encrypt(key, decrypted))
  766.         -- f.write(decrypted)
  767.         f.close()
  768.     end
  769.     function handle.flush()
  770.         f.close()
  771.         f.open(path, "w")
  772.         f.write(encrypt(key, decrypted))
  773.         -- f.write(decrypted)
  774.     end
  775.     function handle.write(val)
  776.         decrypted = decrypted .. string.char(val)
  777.     end
  778.     return handle
  779. end
  780.  
  781. function fs.open(path, mode)
  782.     if not checkPath(path) then
  783.         error("Access denied")
  784.     elseif not shouldEncrypt(path) then
  785.         return oldFs.open(path, mode)
  786.     elseif mode == "r" then
  787.         return openr(path)
  788.     elseif mode == "w" then
  789.         return openw(path)     
  790.     elseif mode == "a" then
  791.         return opena(path) 
  792.     elseif mode == "rb" then
  793.         return openrb(path)
  794.     elseif mode == "wb" then
  795.         return openwb(path)
  796.     elseif mode == "ab" then
  797.         return openab(path)
  798.     end
  799. end
  800.  
  801. os.pullEvent = oldPullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement