Advertisement
Guest User

m1m

a guest
May 12th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. function rKey()
  2.   local key = math.randomseed(os.time() * (10^9))
  3.   key = math.random(0,9999999999)
  4.   key = string.format("%x", key)
  5.   return key
  6. end
  7.  
  8.  
  9. function hash(pw, key)
  10.  
  11.   local pw = pw
  12.   local key = key
  13.  
  14.   local ascii   = {}
  15.   local ascii_2 = {}
  16.  
  17.   for i = 1, #key do
  18.     ascii[i] = string.byte(key, i)
  19.   end
  20.  
  21.   for j = 1, #pw do
  22.     ascii_2[j] = string.byte(pw, j)
  23.   end
  24.  
  25.   local n = string.reverse(table.concat(ascii))
  26.         n = tonumber(n)
  27.   local m = string.reverse(table.concat(ascii_2))
  28.         m = tonumber(m)
  29.  
  30.   local key_n = math.randomseed(n)
  31.         key_n = math.random(0,9999999999)
  32.   local key_m = math.randomseed(m)
  33.         key_m = math.random(0,9999999999)
  34.  
  35.   local mod_n = key_n % #key
  36.   local mod_m = key_m % #pw
  37.  
  38.   local xor = bit.bxor(key_n, key_m)
  39.   local hex = string.format("%x", xor * (mod_n+mod_m+1))
  40.  
  41.   return hex
  42.  
  43. end
  44.  
  45. function store(user, key, pw)
  46.  
  47.   local user = user
  48.   local key = key
  49.   local pw = pw
  50.  
  51.   if fs.exists("/.users") == false then
  52.     fs.makeDir("/.users")
  53.   end
  54.  
  55.   local t = {"/.users/."}
  56.   table.insert(t, user)
  57.   t = table.concat(t)
  58.  
  59.   if fs.exists(t) == false then
  60.     local x = fs.open(t, "w")
  61.     x.writeLine(key)
  62.     x.writeLine(pw)
  63.     x.close()
  64.     return true
  65.   else
  66.     printError("Username already taken")
  67.     sleep(1)
  68.     term.clear()
  69.     term.setCursorPos(1,1)
  70.     return false
  71.   end
  72.  
  73. end
  74.  
  75. function login()
  76.   term.setTextColor(colors.orange)
  77.   print("Logging in!")
  78.   term.setTextColor(colors.white)
  79.   local a, b = 0, 0
  80.   local t
  81.   local u
  82.  
  83.   while true do
  84.    
  85.     term.write("Username: ")
  86.     u = read()
  87.     t = {"/.users/."}
  88.     table.insert(t,u)
  89.     t = table.concat(t)
  90.     print(t)
  91.    
  92.     if fs.exists(t) == false then
  93.       printError("User doesn't exist")
  94.       a = a + 1
  95.       print("Attempt: "..a)
  96.       os.sleep(2)
  97.       term.clear()
  98.       term.setCursorPos(1,1)
  99.       if a >= 3 then
  100.         printError("Shutting down")
  101.         os.sleep(2)
  102.         os.shutdown()
  103.       end
  104.     else
  105.       break
  106.     end
  107.   end
  108.    
  109.    
  110.   while true do
  111.    
  112.     local file = fs.open(t, "r")
  113.     term.write("Password: ")
  114.     local p = read('*')
  115.     local nKey = file.readLine()
  116.     local key = nKey
  117.     nKey = file.readLine()
  118.     local pw = nKey
  119.     local comp = hash(p, key)
  120.    
  121.     if pw ~= comp then
  122.       printError("Wrong password!")
  123.       os.sleep(2)
  124.       login()
  125.     else
  126.       term.setTextColor(colors.green)
  127.       print("Succesfully logged in!")
  128.       break
  129.     end
  130.   end
  131. end
  132.  
  133.  
  134. function register()
  135.   while true do
  136.     term.setTextColor(colors.orange)
  137.     print("Registering!")
  138.     term.setTextColor(colors.white)
  139.     term.write("Username: ")
  140.     local x = read()
  141.     local y = rKey()
  142.     term.write("Password: ")
  143.     local z = read()
  144.     local z = hash(z, y)
  145.     print(z)
  146.     if store(x,y,z) then
  147.       term.setTextColor(colors.green)
  148.       print("Succesfully registered")
  149.       break
  150.     end
  151.   end
  152.   login()
  153. end
  154.  
  155. register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement