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

Making registery for my password login system.

Started by vyse, 06 October 2014 - 05:13 AM
vyse #1
Posted 06 October 2014 - 07:13 AM
hello,

Today i was trying to make a registery for my password login program only thing is im totally new at this stuff so im not sure how to make the registery.. i have the login all down good even added effects
but i wanted to take one step forward and make it so when you first run file/startup it will ask you what you want your username to be then your password also when you run the program again i need it to not re-register a new user.

i was thinking of making it part of startup and then have a if true/false statment so that it wont run that part of the program and go straight into logging in. so this makes is unhackable also the registery program on a disk so that i can add people to my computer if i want to also use that to edit my username and password.



os.pullEvent = os.pullEventRaw
term.setBackgroundColour(colours.white)
term.clear()
term.setTextColour(colours.grey)
local user = ""
local pass = ""
local i = 2
term.setCursorPos(3, 3)
print("			   running Applied-TechSecureure Startup 5.5V)
sleep(3)
term.clear()
term.setCursorPos(1, 1)
print("			Applied-TechSecure Startup")
term.setCursorPos(2, 2)
write("				  Username: ")
input = read("*")
if input == user then
else
term.setCursorPos(6, 8)
print("			   Please Wait...")
sleep(3)
term.setCursorPos(6, 8)
print("		   Username is incorrect")
sleep(3)
term.clear()
shell.run("startup")
end
term.setCursorPos(3, 3)
write("					 Password: ")
input = read("*")
term.clear()
term.setCursorPos(6, 8)
print("			   Please Wait...")
sleep(3)
if input == pass then
else
term.setCursorPos(6, 8)
print("		   Password is incorrect")
sleep(3)
term.clear()
term.setCursorPos(6, 8)
print("				Try Again Later")
sleep(4)
shell.run("startup")
end
NOTE: this is my username and login with no registery


local userFile
local passwordFile
local users = {}
local passwords = {}
local passwordEntered
local userEntered
local userNum = 0
local lineRead
local registerUser = ""
local registerPassword = ""
local confirmPassword = ""
local regUser
local regPassword
local curY1
local curY2
function RegisterUser()
   readUsersAndPasswords()
   textutils.slowPrint("Please enter your desired username: ", 20)
   registerUser = read()
   RegisterPassword()
end
function RegisterPassword()
   textutils.slowPrint("Please enter your desired password: ", 20)
   registerPassword = read("*")
   textutils.slowPrint("Please confirm your password: ", 20)
   confirmPassword = read("*")
   if registerPassword == confirmPassword then
		  textutils.slowPrint("Passwords match.", 20)
		  fs.makeDir("passwordFolder")
		  regUser = fs.open("passwordFolder/users","a")
		  regPassword = fs.open("passwordFolder/passwords","a")
		
		  regUser.writeLine(registerUser)
		  regPassword.writeLine(registerPassword)
		
		  regUser.close()
		  regPassword.close()
		
		  readUsersAndPasswords()
		  textutils.slowPrint("User registered.", 20)
   else
		  textutils.slowPrint("Passwords missmatch. Please try again: ", 20)
		  regUser.close()
		  regPassword.close()
		  RegisterPassword()
   end
end
function readUsersAndPasswords()
   fs.makeDir("passwordFolder")
   regUser = fs.open("passwordFolder/users","a")
   regPassword = fs.open("passwordFolder/passwords","a")
   regUser.close()
   regPassword.close()
   users = {}
   passwords = {}
   userFile = fs.open("passwordFolder/users","r")
   passwordFile = fs.open("passwordFolder/passwords","r")
   lineRead = userFile.readLine()
   repeat
		  table.insert(users,lineRead)
		  lineRead = userFile.readLine()
   until lineRead == nil
   lineRead = passwordFile.readLine()
   repeat
		  table.insert(passwords,lineRead)
		  lineRead = passwordFile.readLine()
   until lineRead == nil
   passwordFile.close()
   userFile.close()
end
function password()
   textutils.slowPrint("Please enter your password "..users[userNum], 20)
   passwordEntered = read("*")
   if passwordEntered == passwords[userNum] then
		  term.clear()
		  term.setCursorPos(1,1)
		  textutils.slowPrint("Password match.", 20)
		  textutils.slowPrint("Welcome "..users[userNum], 20)
   elseif passwordEntered == "retry" then
		  username()
   else
		  term.clear()
		  term.setCursorPos(1,1)
		  textutils.slowPrint("Password missmatch. Please try again.", 20)
		  textutils.slowPrint("Type 'retry' to enter a new username.", 20)
		  password()
   end
end
function username()
   userNum = 0
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Welcome", 20)
   sleep(1)
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Please enter your username", 20)
   userEntered = read()
   for i=1, #users do
		  if userEntered == users[i] then
		  userNum = i
				 break
		  end
   end
   if userNum == 0 then
		  textutils.slowPrint("Username could not be recognized. Please try again", 20)
		  sleep(1)
		  username()
   else
		  term.clear()
		  term.setCursorPos(1,1)
		  textutils.slowPrint("Username recognized.", 20)
		  sleep(1)
		  term.clear()
		  term.setCursorPos(1,1)
		  password()
   end
end
readUsersAndPasswords()
if users[1] == nil then
   textutils.slowPrint("Welcome to your new password program!", 20)
   textutils.slowPrint("Please register your admin user.", 20)
   textutils.slowPrint("This user will be the admin for this system.", 20)
   textutils.slowPrint("The admin is the only one with full access", 20)
   textutils.slowPrint("to this system.", 20)
   RegisterUser()
end


username()
if userNum ~= 1 then

end

NOTE: i did get this from another person but i was going to edit it for it to be compatible with my login program

knowing this i will have to redo my whole login program which im fine with but i need a bit of understanding on the registering part cause this one doesn't work.
Edited on 06 October 2014 - 05:18 AM
Dragon53535 #2
Posted 06 October 2014 - 07:21 AM
Look into the fs API to save the username and password into a file (Wiki: Here. Tutorial: Here.), and then so that any old shmuck can't easily read the password i would run it through a Hash like Sha-256. (Here.)

And because i'm feeling generous you could easily do this.

if not fs.exists("Userinfo") then --#If the file that we're saving the username and pass into doesn't exist then we need to make it
  term.clear()
  term.setCursorPos(1,1)
  write("Enter desired username: ")
  local newuser = read()
  write("Enter desired password: ")
  local newpass = read("*")
  local file = fs.open("Userinfo","w")
  file.writeLine(newuser)
  file.writeLine(newpass)
  file.close()
end --#Since we're going to run the lock anyways, we don't need an else
local file = fs.open("Userinfo","r") --# Since we already know the file exists, and will have to exist, then we're good to open and read
local user = file.readLine()--#Reads the first line, which we saved the username on
local pass = file.readLine() --#Reads the second line which contains the password
file.close()
--#Good to ask for username and password.
Just put that at the top of your code, inside the os.pullEvent = os.pullEventRaw of course :P/>

Btw you should edit this line

print("				    running Applied-TechSecureure Startup 5.5V)
To this

print("				    running Applied-TechSecureure Startup 5.5V")
Edited on 06 October 2014 - 05:32 AM
vyse #3
Posted 06 October 2014 - 07:39 AM
lol… thank you.. that was in my notepad… didnt fix it up :P/> but thank you for the tip! it will help out lots! question don't you need the program to save the file? i see that it opens it but what about saving?
Dragon53535 #4
Posted 06 October 2014 - 07:40 AM
It already does and is part of the fs API, i do believe that the file.close() closes it, however i don't exactly know of that fact.
vyse #5
Posted 06 October 2014 - 08:03 AM
Thank you so much! i was able to make it register and now all i need is the startup program read the userinfo file for its password thanks!
Edited on 06 October 2014 - 06:22 AM
Dragon53535 #6
Posted 06 October 2014 - 10:32 AM
That's what i gave you on the post, that second bit stores it inside the user and pass variables