minecraftwarlock

CCLock

Dec 15th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.47 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 PWD_SALT_HASH_COUNT = 4
  423. local PWD_HASH_COUNT = 29
  424. local KEY_HASH_COUNT = 19
  425. local PASSWORD_FILE = "/.pwd.pwd"
  426. local PASSWORD_FILE_NAME = ".pwd.pwd"
  427.  
  428. local function newSalt()
  429.     return hashVal("", os.time() .. os.day(), PWD_SALT_HASH_COUNT)
  430. end
  431.  
  432. local key = ""
  433.  
  434. local oldFs = {}
  435.  
  436. for k, v in pairs(fs) do
  437.     oldFs[k] = v
  438. end
  439.  
  440. local function checkPath(path)
  441.     return not path:find(PASSWORD_FILE_NAME)
  442. end
  443.  
  444. local function shouldEncrypt(file)
  445.     file = oldFs.combine("", file)
  446.     return not oldFs.isReadOnly(file)
  447.     and (oldFs.getDrive(file) == "hdd" or (oldFs.getDrive(file) == nil and file:sub(1, 4) ~= "rom/" and (not file:find("^disk%d*/"))))
  448.     and (oldFs.getName(file) ~= "startup" or oldFs.getDir(file) ~= "")
  449.     and file ~= PASSWORD_FILE_NAME
  450.     and file ~= PASSWORD_FILE
  451. end
  452.  
  453. local function encryptFile(file)
  454.     if fs.isDir(file) then
  455.         for k, v in ipairs(fs.list(file)) do
  456.             if shouldEncrypt(file .. "/" .. v) then
  457.                 encryptFile(file .. "/" .. v)
  458.             end
  459.         end
  460.     else
  461.         local f = oldFs.open(file, "r")
  462.         local encrypted = encrypt(key, f.readAll())
  463.         f.close()
  464.         f = oldFs.open(file, "w")
  465.         f.write(encrypted)
  466.         f.close()
  467.     end
  468. end
  469.  
  470. local oldPullEvent = os.pullEvent
  471.  
  472. os.pullEvent = os.pullEventRaw
  473.  
  474. if fs.exists(PASSWORD_FILE) then
  475.     local f = fs.open(PASSWORD_FILE, "r")
  476.     local hash = f.readLine()
  477.     local salt = f.readLine()
  478.     f.close()
  479.     print("Enter password")
  480.     local input = read("*")
  481.     while hashVal(input, salt, PWD_HASH_COUNT) ~= hash do
  482.         print("Incorrect password")
  483.         input = read("*")
  484.     end
  485.     key = hashVal(input, salt, KEY_HASH_COUNT)
  486. else
  487.     print("Enter a new password")
  488.     local input = read("*")
  489.     while input:len() < 8 do
  490.         print("Password must be 8 characters or more")
  491.         input = read("*")
  492.     end
  493.     local salt = newSalt()
  494.     local hash = hashVal(input, salt, PWD_HASH_COUNT)
  495.     math.randomseed(os.day())
  496.     local f = fs.open(PASSWORD_FILE, "w")
  497.     f.writeLine(hash)
  498.     f.writeLine(salt)
  499.     f.close()
  500.    
  501.     key = hashVal(input, salt, KEY_HASH_COUNT)
  502.     encryptFile("")
  503.    
  504. end
  505.  
  506. function fs.list(path)
  507.     local t = oldFs.list(path)
  508.     local t2 = {}
  509.     for k, v in ipairs(t) do
  510.         if checkPath(v) then
  511.             table.insert(t2, v)
  512.         end
  513.     end
  514.     return t2
  515. end
  516.  
  517. function fs.exists(path)
  518.     return checkPath(path) and oldFs.exists(path)
  519. end
  520.  
  521. function fs.getDrive(path)
  522.     if not checkPath(path) then
  523.         return nil
  524.     else
  525.         return oldFs.getDrive(path)
  526.     end
  527. end
  528.  
  529. function fs.getSize(path)
  530.     if not checkpath(path) then
  531.         error("Access denied")
  532.     else
  533.         return oldFs.getSize(path)
  534.     end
  535. end
  536.  
  537. function fs.move(fromPath, toPath)
  538.     if checkPath(fromPath) and checkPath(toPath) then
  539.         oldFs.move(fromPath, toPath)
  540.     else
  541.         error("Access denied")
  542.     end
  543. end
  544.  
  545. function fs.copy(fromPath, toPath)
  546.     if checkPath(fromPath) and checkPath(toPath) then
  547.         oldFs.copy(fromPath, toPath)
  548.     else
  549.         error("Access denied")
  550.     end
  551. end
  552.  
  553. function fs.delete(path)
  554.     if checkPath(path)then
  555.         oldFs.delete(path)
  556.     else
  557.         error("Access denied")
  558.     end
  559. end
  560.  
  561. if oldFs.find then
  562.     function fs.find(path)
  563.         local t = oldFs.find(path)
  564.         local t2 = {}
  565.         for k, v in ipairs(t) do
  566.             if checkPath(v) then
  567.                 table.insert(t2, v)
  568.             end
  569.         end
  570.         return t2
  571.     end
  572. end
  573.  
  574. if oldFs.complete then
  575.     function fs.complete(partial, path, includeFiles, includeSlashes)
  576.         local t = oldFs.complete(partial, path, includeFiles, includeSlashes)
  577.         local t2 = {}
  578.         for k, v in ipairs(t) do
  579.             if checkPath(v) then
  580.                 table.insert(t2, v)
  581.             end
  582.         end
  583.         return t2
  584.     end
  585. end
  586.  
  587. local function openr(path)
  588.     local f = oldFs.open(path, "r")
  589.     local idx = 1
  590.     local decrypted = decrypt(key, f.readAll())
  591.     -- local decrypted = f.readAll()
  592.    
  593.     local handle = {}
  594.     function handle.close()
  595.         f.close()
  596.     end
  597.     function handle.readLine()
  598.         local i = decrypted:find("\n", idx) or #decrypted + 1
  599.         if idx > #decrypted then
  600.             return nil
  601.         end
  602.         local s = decrypted:sub(idx, i - 1)
  603.         idx = i + 1
  604.         return s
  605.     end
  606.     function handle.readAll()
  607.         if idx > #decrypted then
  608.             return nil
  609.         end
  610.         local s = decrypted:sub(idx)
  611.         if s:sub(-1) == "\n" then
  612.             return s:sub(1, -2)
  613.         end
  614.         return s
  615.     end
  616.     return handle
  617. end
  618.  
  619. local function openw(path)
  620.     local f = oldFs.open(path, "w")
  621.     local decrypted = ""
  622.    
  623.     local handle = {}
  624.     function handle.close()
  625.         f.write(encrypt(key, decrypted))
  626.         -- f.write(decrypted)
  627.         f.close()
  628.     end
  629.     function handle.flush()
  630.         f.close()
  631.         f.open(path, "w")
  632.         f.write(encrypt(key))
  633.         -- f.write(decrypted)  
  634.     end
  635.     function handle.writeLine(val)
  636.         decrypted = decrypted .. val .. "\n"
  637.     end
  638.     function handle.write(val)
  639.         decrypted = decrypted .. val
  640.     end
  641.     return handle
  642. end
  643.  
  644. local function opena(path)
  645.     local f = oldFs.open(path, "r")
  646.     local decrypted = decrypt(key, f.readAll())
  647.     -- local decrypted = f.readAll()
  648.     f.close()
  649.     f = oldFs.open(path, "r")
  650.    
  651.     local handle = {}
  652.     function handle.close()
  653.         f.write(encrypt(key, decrypted))
  654.         -- f.write(decrypted)
  655.         f.close()
  656.     end
  657.     function handle.flush()
  658.         f.close()
  659.         f.open(path, "w")
  660.         f.write(encrypt(key, decrypted))
  661.         -- f.write(decrypted)      
  662.     end
  663.     function handle.writeLine(val)
  664.         decrypted = decrypted .. val .. "\n"
  665.     end
  666.     function handle.write(val)
  667.         decrypted = decrypted .. val
  668.     end
  669.     return handle
  670. end
  671.  
  672. local function openrb(path)
  673.     local f = oldFs.open(path, "r")
  674.     local idx = 1
  675.     local decrypted = decrypt(key, f.readAll())
  676.     -- local decrypted = f.readAll()
  677.    
  678.     local handle = {}
  679.     function handle.close()
  680.         f.close()
  681.     end
  682.     function handle.read()
  683.         idx = idx + 1
  684.         return string.byte(decrypted:sub(idx - 1, idx - 1))
  685.     end
  686.     return handle
  687. end
  688.  
  689. local function openwb(path)
  690.     local f = oldFs.open(path, "w")
  691.     local decrypted = ""
  692.    
  693.     local handle = {}
  694.     function handle.close()
  695.         f.write(encrypt(key, decrypted))
  696.         -- f.write(decrypted)
  697.         f.close()
  698.     end
  699.     function handle.flush()
  700.         f.close()
  701.         f.open(path, "w")
  702.         f.write(encrypt(key, decrypted))
  703.         -- f.write(decrypted)
  704.     end
  705.     function handle.write(val)
  706.         decrypted = decrypted .. string.char(val)
  707.     end
  708.     return handle
  709. end
  710.  
  711. local function openab(path)
  712.     local f = oldFs.open(path, "r")
  713.     local decrypted = decrypt(key, f.readAll())
  714.     -- local decrypted = f.readAll()
  715.     f.close()
  716.     f = oldFs.open(path, "r")
  717.    
  718.     local handle = {}
  719.     function handle.close()
  720.         f.write(encrypt(key, decrypted))
  721.         -- f.write(decrypted)
  722.         f.close()
  723.     end
  724.     function handle.flush()
  725.         f.close()
  726.         f.open(path, "w")
  727.         f.write(encrypt(key, decrypted))
  728.         -- f.write(decrypted)
  729.     end
  730.     function handle.write(val)
  731.         decrypted = decrypted .. string.char(val)
  732.     end
  733.     return handle
  734. end
  735.  
  736. function fs.open(path, mode)
  737.     if not checkPath(path) then
  738.         error("Access denied")
  739.     elseif not shouldEncrypt(path) then
  740.         return oldFs.open(path, mode)
  741.     elseif mode == "r" then
  742.         return openr(path)
  743.     elseif mode == "w" then
  744.         return openw(path)     
  745.     elseif mode == "a" then
  746.         return opena(path) 
  747.     elseif mode == "rb" then
  748.         return openrb(path)
  749.     elseif mode == "wb" then
  750.         return openwb(path)
  751.     elseif mode == "ab" then
  752.         return openab(path)
  753.     end
  754. end
  755.  
  756. os.pullEvent = oldPullEvent
Add Comment
Please, Sign In to add comment