Advertisement
Guest User

kemguard2

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.72 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. kg = function ()
  192.   clear()
  193.   print("[KEMGuard] - V 2.0")
  194. end
  195.  
  196. click = function ()
  197.   event,key,x,y = os.pullEvent("mouse_click")
  198.   x = tonumber(x)
  199.   y = tonumber(y)
  200.   a = {key,x,y}
  201.   return a
  202. end
  203.  
  204. plast = function ()
  205.   kg()
  206.   print(" ")
  207.   print("   [Existing User]")
  208.   print(" ")
  209.   print("   [New User]")
  210. end
  211.  
  212. menu = function ()
  213.   while true do
  214.     plast()
  215.     b = click()
  216.     if b[3] == 3 and b[2] > 3 and b[2] < 19 then
  217.       if exist() == 1 then
  218.         break
  219.       end
  220.     elseif b[3] == 5 and b[2] > 3 and b[2] < 14 then
  221.       new()
  222.     end
  223.   end
  224. end
  225.  
  226. exist = function ()
  227.   kg()
  228.   print("")
  229.   write("Username : ")
  230.   luse = read()
  231.   print("")
  232.   write("Password : ")
  233.   lpas = read()
  234.   if fs.exists("/pash/"..luse) then
  235.     pa = fs.open("/pash/"..luse,"r")
  236.     lpa = pa:readLine()
  237.     if sha256(lpas) == lpa then
  238.       eval = 1
  239.       clear()
  240.       print("Welcome "..luse.."!")
  241.       sleep(2)
  242.     else
  243.       clear()
  244.       print("Error : Invalid Login")
  245.       sleep(2)
  246.       eval = 2
  247.     end
  248.   else
  249.   clear()
  250.   print("Error : Invalid Login")
  251.   sleep(2)
  252.   eval = 2
  253.   end
  254.   return eval
  255. end
  256.  
  257. new = function ()
  258.   kg()
  259.   print(" ")
  260.   write("Username : ")
  261.   use = read()
  262.   print(" ")
  263.   write("Password : ")
  264.   pas1 = read()
  265.   print(" ")
  266.   write("Confirm : ")
  267.   pas2 = read()
  268.   if pas1 == pas2 and not fs.exists("/pash/"..use) then
  269.     pa = fs.open("/pash/"..use,"w")
  270.     pa.writeLine(sha256(pas1))
  271.     pa:close()
  272.     clear()
  273.     print("Account Made!")
  274.     sleep(2)
  275.   else
  276.     if pas1 ~= pas2 then
  277.       clear()
  278.       print("Error : Passwords Do Not Match ")
  279.       sleep(2)
  280.     else
  281.       clear()
  282.       print("Error : Username Taken")
  283.       sleep(2)
  284.     end
  285.   end  
  286. end
  287.  
  288. if not fs.exists("/pash") then
  289.   fs.makeDir("/pash")
  290. end
  291.  
  292. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement