Advertisement
Guest User

kemguard

a guest
May 24th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.75 KB | None | 0 0
  1. local MOD = 2^32
  2. local MODM = MOD-1
  3. os.pullEvent = os.pullEventRaw
  4.  
  5. local function memoize(f)
  6.         local mt = {}
  7.         local t = setmetatable({}, mt)
  8.         function mt:__index(k)
  9.                 local v = f(k)
  10.                 t[k] = v
  11.                 return v
  12.         end
  13.         return t
  14. end
  15.  
  16. local function make_bitop_uncached(t, m)
  17.         local function bitop(a, b)
  18.                 local res,p = 0,1
  19.                 while a ~= 0 and b ~= 0 do
  20.                         local am, bm = a % m, b % m
  21.                         res = res + t[am][bm] * p
  22.                         a = (a - am) / m
  23.                         b = (b - bm) / m
  24.                         p = p*m
  25.                 end
  26.                 res = res + (a + b) * p
  27.                 return res
  28.         end
  29.         return bitop
  30. end
  31.  
  32. local function make_bitop(t)
  33.         local op1 = make_bitop_uncached(t,2^1)
  34.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  35.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  36. end
  37.  
  38. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  39.  
  40. local function bxor(a, b, c, ...)
  41.         local z = nil
  42.         if b then
  43.                 a = a % MOD
  44.                 b = b % MOD
  45.                 z = bxor1(a, b)
  46.                 if c then z = bxor(z, c, ...) end
  47.                 return z
  48.         elseif a then return a % MOD
  49.         else return 0 end
  50. end
  51.  
  52. local function band(a, b, c, ...)
  53.         local z
  54.         if b then
  55.                 a = a % MOD
  56.                 b = b % MOD
  57.                 z = ((a + b) - bxor1(a,b)) / 2
  58.                 if c then z = bit32_band(z, c, ...) end
  59.                 return z
  60.         elseif a then return a % MOD
  61.         else return MODM end
  62. end
  63.  
  64. local function bnot(x) return (-1 - x) % MOD end
  65.  
  66. local function rshift1(a, disp)
  67.         if disp < 0 then return lshift(a,-disp) end
  68.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  69. end
  70.  
  71. local function rshift(x, disp)
  72.         if disp > 31 or disp < -31 then return 0 end
  73.         return rshift1(x % MOD, disp)
  74. end
  75.  
  76. local function lshift(a, disp)
  77.         if disp < 0 then return rshift(a,-disp) end
  78.         return (a * 2 ^ disp) % 2 ^ 32
  79. end
  80.  
  81. local function rrotate(x, disp)
  82.     x = x % MOD
  83.     disp = disp % 32
  84.     local low = band(x, 2 ^ disp - 1)
  85.     return rshift(x, disp) + lshift(low, 32 - disp)
  86. end
  87.  
  88. local k = {
  89.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  90.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  91.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  92.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  93.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  94.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  95.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  96.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  97.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  98.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  99.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  100.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  101.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  102.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  103.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  104.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  105. }
  106.  
  107. local function str2hexa(s)
  108.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  109. end
  110.  
  111. local function num2s(l, n)
  112.         local s = ""
  113.         for i = 1, n do
  114.                 local rem = l % 256
  115.                 s = string.char(rem) .. s
  116.                 l = (l - rem) / 256
  117.         end
  118.         return s
  119. end
  120.  
  121. local function s232num(s, i)
  122.         local n = 0
  123.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  124.         return n
  125. end
  126.  
  127. local function preproc(msg, len)
  128.         local extra = 64 - ((len + 9) % 64)
  129.         len = num2s(8 * len, 8)
  130.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  131.         assert(#msg % 64 == 0)
  132.         return msg
  133. end
  134.  
  135. local function initH256(H)
  136.         H[1] = 0x6a09e667
  137.         H[2] = 0xbb67ae85
  138.         H[3] = 0x3c6ef372
  139.         H[4] = 0xa54ff53a
  140.         H[5] = 0x510e527f
  141.         H[6] = 0x9b05688c
  142.         H[7] = 0x1f83d9ab
  143.         H[8] = 0x5be0cd19
  144.         return H
  145. end
  146.  
  147. local function digestblock(msg, i, H)
  148.         local w = {}
  149.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  150.         for j = 17, 64 do
  151.                 local v = w[j - 15]
  152.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  153.                 v = w[j - 2]
  154.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  155.         end
  156.  
  157.         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]
  158.         for i = 1, 64 do
  159.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  160.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  161.                 local t2 = s0 + maj
  162.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  163.                 local ch = bxor (band(e, f), band(bnot(e), g))
  164.                 local t1 = h + s1 + ch + k[i] + w[i]
  165.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  166.         end
  167.  
  168.         H[1] = band(H[1] + a)
  169.         H[2] = band(H[2] + b)
  170.         H[3] = band(H[3] + c)
  171.         H[4] = band(H[4] + d)
  172.         H[5] = band(H[5] + e)
  173.         H[6] = band(H[6] + f)
  174.         H[7] = band(H[7] + g)
  175.         H[8] = band(H[8] + h)
  176. end
  177.  
  178. local function sha256(msg)
  179.         msg = preproc(msg, #msg)
  180.         local H = initH256({})
  181.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  182.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  183.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  184. end
  185.  
  186. clear = function()
  187.   term.clear()
  188.   term.setCursorPos(1,1)
  189. end
  190.  
  191. --col = term.setTextColor
  192.  
  193. --colb = term.setBackgroundColor
  194.  
  195. kg = function ()
  196.   clear()
  197.   term.setTextColor(0,0,0)
  198.   term.setBackgroundColor(128,128,128)
  199.   print("[KEMGuard] - V 2.0                                 ")
  200. end
  201.  
  202. click = function ()
  203.   event,key,x,y = os.pullEvent("mouse_click")
  204.   x = tonumber(x)
  205.   y = tonumber(y)
  206.   a = {key,x,y}
  207.   return a
  208. end
  209.  
  210. plast = function ()
  211.   kg()
  212.   print(" ")
  213.   print("   [Existing User]")
  214.   print(" ")
  215.   print("   [New User]")
  216. end
  217. menu = function ()
  218.   while true do
  219.     plast()
  220.     b = click()
  221.     if b[3] == 3 and b[2] > 3 and b[2] < 19 then
  222.       exist()
  223.     elseif b[3] == 5 and b[2] > 3 and b[2] < 14 then
  224.       if new() == 1 then
  225.         break
  226.       end
  227.     end
  228.   end
  229. end
  230.  
  231. exist = function ()
  232.   kg()
  233.   print("")
  234.   write("Username : ")
  235.   luse = read()
  236.   print("")
  237.   write("Password : ")
  238.   lpas = read()
  239.   if fs.exists("/pash/"..luse) then
  240.     pa = io.open("/pash/"..luse,"r")
  241.     lpa = pa:read()
  242.     if sha256(lpas) == lpa then
  243.       eval = 1
  244.     else
  245.       clear()
  246.       print("Error : Invalid Login")
  247.       sleep(2)
  248.       eval = 2
  249.     end
  250.   else
  251.   clear()
  252.   print("Error : Invalid Login")
  253.   sleep(2)
  254.   eval = 2
  255.   end
  256.   return eval
  257. end
  258.  
  259. new = function ()
  260.   kg()
  261.   print(" ")
  262.   write("Username : ")
  263.   use = read()
  264.   print(" ")
  265.   write("Password : ")
  266.   pas1 = read()
  267.   print(" ")
  268.   write("Confirm : ")
  269.   pas2 = read()
  270.   if pas1 == pas2 and not fs.exists("/pash/"..use) then
  271.     pa = io.open("/pash/"..use,"w")
  272.     pa:write(sha256(pas1))
  273.     pa:close()
  274.   else
  275.     if pas1 ~= pas2 then
  276.       clear()
  277.       print("Error : Passwords Do Not Match ")
  278.       sleep(2)
  279.     else
  280.       clear()
  281.       print("Error : Username Taken")
  282.       sleep(2)
  283.     end
  284.   end  
  285. end
  286.  
  287.  if not fs.exists("/pash") then
  288.   fs.makeDir("/pash")
  289. end
  290.  
  291. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement