Advertisement
Guest User

auth.lua

a guest
May 11th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.02 KB | None | 0 0
  1. require("/scripts/auth/sha256")
  2.  
  3. local oldPull = os.pullEvent
  4. os.pullEvent = os.pullEventRaw
  5.  
  6. function resetCursor(x, y)
  7.  term.clear()
  8.  term.setCursorPos(x, y)
  9. end
  10.  
  11. function verify_user(username, password)
  12.     for line in io.lines("/.password") do
  13.         i = string.find(line, "|")
  14.         if (i ~= nil and string.sub(line, 1, i-1) == username) then
  15.    digest = digestStr(password)
  16.             if string.sub(line, i+1) == digest then
  17.     resetCursor(1, 1)
  18.     print("Welcome back!")
  19.                 return true
  20.             else
  21.     resetCursor(1, 1)
  22.     print("Wrong password")
  23.     return false
  24.             end
  25.         end
  26.     end
  27.  resetCursor(1, 1)
  28.  print("User '"..username.."' does not exist")
  29.  return false
  30. end
  31.  
  32. local auth = false
  33. resetCursor(1, 1)
  34. while not auth do
  35.     io.write("Username: ")
  36.  local username = string.gsub(read(), "^ +", "")
  37.  username = string.gsub(username, " +$", "")
  38.     io.write("Password: ")
  39.     local password = read("*")
  40.  auth = verify_user(username, password)
  41.  if not auth then
  42.   sleep(2)
  43.   resetCursor(1, 1)
  44.  end
  45. end
  46.  
  47. os.pullEvent = oldPull
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement