morganamilo

computer craft startup login with password hashing v1.2

May 7th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. -- by morganamilo ([email protected])
  2.  
  3. --to add to startup run this program in startup (shell.run('file name'))
  4. --or name this file startup
  5.  
  6. --usernames = the file name at /users/
  7. --passwords is data in the file (hashed)
  8.  
  9. --downloads my hash program if not on pc
  10.  
  11. --'ProgramName user1 pass1' creates an account on the turtle with
  12. --username: user1
  13. --password: pass1
  14.  
  15. if fs.exists('/hash') == false then
  16.   print('no hash program found, downloading...')
  17.   shell.run('pastebin get VPuGKaBy /hash')
  18. end
  19.  
  20. local oldPull = os.pullEvent
  21. os.pullEvent = os.pullEventRaw
  22.  
  23. os.loadAPI('/hash')
  24.  
  25. if fs.exists('/users/') == false then
  26.     shell.run('mkdir /users/')
  27. end
  28.  
  29. local args = {...}
  30.  
  31. if args[1] ~= 'new' and args[1] ~= 'delete' and args[1] ~= 'login' and args[1] ~= nil then
  32.   local name = shell.getRunningProgram()
  33.   print('Usages:\n' .. name .. ' login\n' .. name.. ' new <username> <password>\n' .. name .. ' delete <username>')
  34.   error()
  35. end
  36.  
  37. function logIn()
  38.   local pass
  39.  
  40.   if fs.list('/users/')[1] == null and (args[1] == 'login' or args[1] == nil) then
  41.     print('cant begin login, no accounts exis')
  42.     local name = shell.getRunningProgram()
  43.     print('Usages:\n' .. name .. ' login\n' .. name.. ' new <username> <password>\n' .. name .. ' delete <username>')
  44.     os.pullEvent = oldPull
  45.     error()
  46.   end
  47.  
  48.   while true do
  49.     io.write('username: ')
  50.     local inUser = read()
  51.     io.write('password: ')
  52.     local inPass = read('*')
  53.     if inPass ~= '' then
  54.       inPass = hash.sha256(inPass)
  55.       end
  56.    
  57.     local empty = ""
  58.    
  59.     if inUser ~= empty and inPass ~= empty and fs.exists('/users/' .. inUser) then
  60.       local userFile = fs.open('/users/' .. inUser, 'r')
  61.       pass = userFile.readAll()
  62.       userFile.close()
  63.     end
  64.  
  65.       if pass == inPass then
  66.         break
  67.       else
  68.         print('username or password is incorrect\n')
  69.         inPass = empty
  70.         inUser = empty  
  71.       end
  72.    
  73.   end
  74.  
  75.   term.clear()
  76.   term.setCursorPos(1, 1)
  77.   print('Welcome')
  78. end
  79.  
  80. function newUser(user, pass)
  81.   if fs.exists('/users/' .. user) then
  82.     print('user already exists')
  83.   else
  84.     local userFile = fs.open('/users/' .. user, 'w')
  85.     pass = hash.sha256(pass)
  86.     userFile.write(pass)
  87.     userFile.close()
  88.   end
  89. end
  90.  
  91. function deleteUser(user)
  92.   if fs.exists('/users/' .. user) ~= true then
  93.     print('user doesnt exist')
  94.   else
  95.     fs.delete('/users/' .. user)
  96.     print('done\n')
  97.   end
  98. end
  99.  
  100. if args[1] == 'new' and args[3] ~= nil then
  101.   newUser(args[2],args[3])
  102.  
  103. elseif args[1] == 'login' or args[1] == nil then
  104.   logIn()
  105.  
  106. elseif args[1] == 'delete' and args[2] ~= nil then
  107.   deleteUser(args[2])
  108. end
  109.  
  110. os.unloadAPI('/hash')
  111. os.pullEvent = oldPull
Advertisement
Add Comment
Please, Sign In to add comment