morganamilo

[obsolete] computer craft login with password hashing

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