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

Help with using a variable as a table key

Started by CupricWolf, 11 June 2013 - 11:15 PM
CupricWolf #1
Posted 12 June 2013 - 01:15 AM
Hello,
I am making a very simple (and probably hackable) login system for my single player world. Not that I need it but hey, the more programming a person does the better they get at it. I am trying to use a simple system where a serialized table with usernames and hashed passwords is stored to a file and the login system then unserializes that table and checks the input-ed username and password against that table. But I seem to be having a problem saving the table in the first place. In my code I have set the variable userName to "Happydude11209" and set passwordHash to "xLerx{N7,Ujty:N7>?_@0($" after hashing "password". But then I get an error with the following piece of code.

-- EditUsers lines 98 to 100
print(userName)
print(passwordHash)
passwordTable[userName] = passwordHash -- line 100
That code gives the following output:

Happydude11209
xLerx{N7,Ujty:N7>?_@0($
EditUsers:100: index expected, got nil
Am I doing anything wrong or can I not set string index table entries using a variable to represent the index?
Regards,
Happydude11209
theoriginalbit #2
Posted 12 June 2013 - 01:23 AM
You have not defined the passwordTable variable. Add this and test again.

local passwordTable = {}
crazyguymgd #3
Posted 12 June 2013 - 01:33 AM
Can we see more of your code? Something must be missing somewhere before line 98.
CupricWolf #4
Posted 12 June 2013 - 01:39 AM
http://pastebin.com/UuntpP7p for the whole code. Some of the rest of the code is pretty messy and I'm gonna go through and change them to be nicer but I just wanted to see if the table storage worked. I am using https://github.com/g...master/hash.lua as my hash api. I had also messed up the line numbers, exists on 103. passwordTable is the first thing it defines after loading the hash api.

Edit: Sorry bout the late reply, I had some chores to do.

Edit: Edit: I'm an idiot… I had local passwWordTable = {} but needed local passwordTable = {}. The extra "W" came in when I had changed my mind on capitalization. This now completes without any errors.
theoriginalbit #5
Posted 12 June 2013 - 01:47 AM
What is the current contents of this file: loginPasswords.secure.txt?

The reason I say that is the particular error message you're getting is because the passwordTable variable is nil. Now before you go and say "but I've defined it", well you haven't, because of this line here

passwordTable = textutils.unserialize(passwordFile.readAll())
if textutils fails to unserialize a table, or the file is empty (but exists) it will return nil, thus setting passwordTable to nil… suggested fix:

passwordTable = textutils.unserialize(passwordFile.readAll()) or {}
this way if textutils returns nil, passwordTable will become a new table.
CupricWolf #6
Posted 12 June 2013 - 01:47 AM
Thanks for telling me about passwordTable not being defined and making my look over my code more critically :)/> This now creates the text file, but my login code fails D: so now I need to look over that code to see if there are any weird errors :P/>.
theoriginalbit #7
Posted 12 June 2013 - 01:49 AM
but my login code fails D:
Make sure you're `hashing` the password that has been input by the user…
CupricWolf #8
Posted 12 June 2013 - 01:53 AM
loginPasswords.secure.txt contains

{["Happydude11209"]="rx{N7,U;.Vjty|N7[>^?_@0($",}

The code that checks my logins is whats failing. It returns false always.


function checkLogin(userName, password)
    passwordHash = hash.hash(password)
    if fs.exists("loginPasswords.secure.txt") then
        local passwordFile = fs.open("loginPasswords.secure.txt", "r")
        local passwordTable = textutils.unserialize(passwordFile.readAll())
        passwordFile.close()
        if (passwordTable[username] == passwordHash) then -- Correct word
            return true
        else
            return false
        end
    else
        error("No saved Passwords! Please run \"EditUsers\" and follow its steps.")
    end
end
theoriginalbit #9
Posted 12 June 2013 - 01:55 AM
No I mean when they're logging in. When they press enter you need to get that password, hash it, and then compare.
CupricWolf #10
Posted 12 June 2013 - 01:56 AM
Lol we are just timing (or at least I am) our responses so that they come after a new question :P/>
Note: I have up dated the pastebin (http://pastebin.com/UuntpP7p) to my current working version.

My login script is here if you're curious
Edited on 12 June 2013 - 12:05 AM
theoriginalbit #11
Posted 12 June 2013 - 02:04 AM
oh also I should point out that on first run your script wont work, you spelt the local definition of passwordTable as passwWordTable.

same reason as to why the login script doesn't work, you're doing this passwordTable[username] you should be doing this passwordTable[userName]

Also a slight improvement, instead of this:

if (passwordTable[username] == passwordHash) then
  return true
else
  return false
end
please, please, please, instead do this

return passwordTable[username] == passwordHash
no need to evaluate an if statement (which is a boolean) to then return a boolean, just return the conditional of the if statement.
CupricWolf #12
Posted 12 June 2013 - 02:12 AM
oh also I should point out that on first run your script wont work, you spelt the local definition of passwordTable as passwWordTable.

same reason as to why the login script doesn't work, you're doing this passwordTable[username] you should be doing this passwordTable[userName]

Also a slight improvement, instead of this:

if (passwordTable[username] == passwordHash) then
  return true
else
  return false
end
please, please, please, instead do this

return passwordTable[username] == passwordHash
no need to evaluate an if statement (which is a boolean) to then return a boolean, just return the conditional of the if statement.

Ok thanks! I fixed the "wW" issue in my EditUsers program and edited the paste bin. I will fix that condition and edit this post with the results. Its hard being dyslexic and a programmer >_<

EDIT: This was a success! Thanks for all the help!!

EDIT:EDIT: I was originally going to do some other stuff based on that conditional inside checkLogin() but decided to do that all outside because the name checkLogin (to me) implies that all it does is check the login information you give it, not do other things. But I had already written out that if and got lazy about that when I found the issue that started this thread.
Edited on 12 June 2013 - 12:19 AM