Advertisement
happydude11209

Login script

Jun 12th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. os.loadAPI("hash")
  2.  
  3. local userNameText, passwordStarText, plainTextpassword, attempts, attemptsBeforeFailure, cursorIsOn, typingUserName = "", "", "", 0, 3, true, true
  4. local timerUUID = os.startTimer(0.25)
  5. local messageToUser = ""
  6. local messageToUserTicksLeft = 0
  7.  
  8. for i,v in pairs(rs.getSides()) do
  9.     if (peripheral.getType(v) == "modem") then
  10.         if (rednet.isOpen(v)) then
  11.             rednet.close(v)
  12.         end
  13.     end
  14. end -- To be sure nobody is listening
  15.  
  16. function checkLogin(userName, password)
  17.     -- passwordHash = hash.hash(password)
  18.     passwordHash = hash.sha256(password)
  19.     if fs.exists("loginPasswords.secure.txt") then
  20.         local passwordFile = fs.open("loginPasswords.secure.txt", "r")
  21.         local passwordTable = textutils.unserialize(passwordFile.readAll()) or {}
  22.         passwordFile.close()
  23.         return passwordTable[userName] == passwordHash
  24.     else
  25.         error("No saved Passwords! Please run \"EditUsers\" and follow its steps.")
  26.     end
  27. end
  28.  
  29. while true do
  30.     term.clear()
  31.     term.setCursorPos(1, 1)
  32.     print("Welcome to computer #", os.getComputerID(), ". Please login.")
  33.     if (cursorIsOn) then
  34.         if (typingUserName) then
  35.             print("Username: ", userNameText, "_")
  36.             print("Password: ", passwordStarText)
  37.         else
  38.             print("Username: ", userNameText)
  39.             print("Password: ", passwordStarText, "_")
  40.         end
  41.     else
  42.         print("Username: ", userNameText)
  43.         print("Password: ", passwordStarText)
  44.     end
  45.     print("\n", messageToUser)
  46.    
  47.     local event, return1, return2, return3 = os.pullEventRaw()
  48.     if (event == "char") then
  49.         if (typingUserName) then
  50.             userNameText = userNameText .. return1
  51.         else
  52.             passwordStarText = passwordStarText .. "*"
  53.             plainTextpassword = plainTextpassword .. return1
  54.         end
  55.     elseif (event == "key") then
  56.         if (return1 == 28) then
  57.             if (typingUserName) then
  58.                 typingUserName = false
  59.             else
  60.                 if (checkLogin(userNameText, plainTextpassword)) then
  61.                     if (fs.exists("loginStartup")) then
  62.                         shell.run("loginStartup")
  63.                         return
  64.                     end
  65.                 else
  66.                     attempts = attempts + 1
  67.                     if (attempts >= attemptsBeforeFailure) then
  68.                         print("Login Failed. Shutting Down.")
  69.                         sleep(5)
  70.                         os.shutdown()
  71.                     else
  72.                         messageToUser = "Login Failure #" .. attempts .. "/" .. attemptsBeforeFailure
  73.                         messageToUserTicksLeft = 6
  74.                     end
  75.                 end
  76.             end
  77.         elseif (return1 == 14) then -- delete key
  78.             if (typingUserName) then
  79.                 userNameText = string.sub(userNameText, 1, string.len(userNameText) - 1)
  80.             else
  81.                 passwordStarText = string.sub(passwordStarText, 1, string.len(passwordStarText) - 1)
  82.                 plainTextpassword = string.sub(plainTextpassword, 1, string.len(plainTextpassword) - 1)
  83.             end
  84.         end
  85.     elseif(event == "timer" and timerUUID == return1) then
  86.         if (messageToUserTicksLeft > 0) then
  87.             messageToUserTicksLeft = messageToUserTicksLeft - 1
  88.             if (messageToUserTicksLeft == 0) then
  89.                 messageToUser = ""
  90.             end
  91.         end
  92.         cursorIsOn = not cursorIsOn
  93.         timerUUID = os.startTimer(0.25)
  94.     end
  95. end
  96.  
  97. os.unloadAPI("hash")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement