This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MythicalSora's profile picture

Trying to create a login system

Started by MythicalSora, 09 January 2017 - 09:59 AM
MythicalSora #1
Posted 09 January 2017 - 10:59 AM
Hi, I'm trying to create a login system which saves the passwords to a file. I also want to make something to encrypt the saved password, but I don't know how. Here's my code:

term.clear()
term.setCursorPos(1,1)
local UsrData = fs.exist("/Users/"..UsrName"")

print("Please login:")
local UsrName = read()
print("Checking if"..UsrName"exist...")
if UsrData == true then
fs.open("/Users/"..UsrName,"a")
local fileData = {}
local line = file.reaadLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()
local UserPass = fileData[1]
print(..UsrName", please give me your password")
input = read()
if input == UserPass then
  print("You have succesfully logged in!")
  term.clear()
  term.setCursorPos(1,1)
else
  print("Wrong password!")
  sleep(1)
  shell.run("/login")
else
term.clear()
term.setCursorPos(1,1)
print("I don't know you "..UsrName"")
print("Please, select a password to save:")
password = read()
local file = fs.open("/Users/"..UsrName,"a")
file.writeLine(password)
file.close()
print("I saved your account "..UsrName"")
print("Please login!!")
sleep(1)
shell.run("/login")
end


Edited on 09 January 2017 - 10:05 AM
MythicalSora #2
Posted 09 January 2017 - 12:53 PM
The system is working now, but can someone help me with encrypting/decrypting?
Lupus590 #3
Posted 09 January 2017 - 12:53 PM
There are APIs on the forums which do encryption, I would use one of those.
Exerro #4
Posted 10 January 2017 - 10:54 AM
You don't encrypt passwords, you hash them. A hash is irreversible, meaning nobody will be able to reverse it and get the password. The hash of the password will always be the same, so if you compare the saved hash to the hash of a password entered, they will be the same if the passwords are the same.
Lupus590 #5
Posted 10 January 2017 - 11:02 AM
You may also want to look into salting passwords for increased security. Again, people have made APIs which do this and they can be found on the forums.