Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. os.loadAPI("sha")
  2.  
  3. local passPath = "passwords" --Change this if you want a different password file
  4.  
  5. if not fs.exists(passPath) then --Create a password file
  6. local f = fs.open(passPath, "w")
  7. f.write(textutils.serialize({}))
  8. f.close()
  9. end
  10.  
  11. local function secureInput( mask, prestring )
  12. local l = true
  13. term.setCursorBlink(true)
  14. local prestring = prestring or ""
  15. local str = ""
  16. local sx, sy = term.getCursorPos()
  17. while l do
  18. local e, a, b, c, d = os.pullEventRaw()
  19. if e == "char" then
  20. str = str .. a
  21. elseif e == "key" then
  22. if a == 14 then
  23. str = str:sub(1, -2)
  24. elseif a == 28 then
  25. l = false
  26. term.setBackgroundColour( colours.black )
  27. term.setTextColour( colours.white )
  28. print()
  29. return str
  30. end
  31. end
  32. term.setCursorPos(sx, sy)
  33. term.setBackgroundColour( colours.white )
  34. term.setTextColour( colours.black )
  35. term.clearLine()
  36. if mask then
  37. write( prestring .. " > " .. string.rep(mask, #str) )
  38. else
  39. write( prestring .. " > " .. str )
  40. end
  41. end
  42. end
  43.  
  44. local function getPass( usr )
  45. local f = fs.open(passPath,"r")
  46. local pwds = textutils.unserialize( f.readAll() )
  47. f.close()
  48. if pwds[ usr ] then
  49. return pwds[ usr ]
  50. else
  51. return false
  52. end
  53. end
  54.  
  55. local function login( usr, pwd )
  56. local info = getPass( usr )
  57. if info then
  58. if sha.sha256( pwd .. info.salt ) == info.pwd then
  59. return true
  60. else
  61. return false
  62. end
  63. else
  64. return false
  65. end
  66. end
  67.  
  68. --Login script--
  69. local loop = true
  70. while loop do
  71. local username = secureInput( nil, "username" )
  72. local password = secureInput( "x", "password" )
  73. if login( username, password ) then
  74. print("Login successfull!")
  75. loop = false
  76. break
  77. --DO SOMETHING
  78. else
  79. print("Login failed.")
  80. --REMOVE these 2 lines below so that the loop continues after incorrect login, once proven program works.
  81. loop = false
  82. break
  83. end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement