Jump to content




Help with using a variable as a table key


11 replies to this topic

#1 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 12: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>[email protected]($" 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>[email protected]($
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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 12:23 AM

You have not defined the passwordTable variable. Add this and test again.
local passwordTable = {}


#3 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 12 June 2013 - 12:33 AM

Can we see more of your code? Something must be missing somewhere before line 98.

#4 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 12: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.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 12: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.

#6 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 12: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.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 12:49 AM

View Posthappydude11209, on 12 June 2013 - 12:47 AM, said:

but my login code fails D:
Make sure you're `hashing` the password that has been input by the user...

#8 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 12:53 AM

loginPasswords.secure.txt contains
{["Happydude11209"]="rx{N7,U;.Vjty|N7[>^[email protected]($",}

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


#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 12: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.

#10 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 12: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 by happydude11209, 12 June 2013 - 01:05 AM.


#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2013 - 01: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.

#12 CupricWolf

  • Members
  • 86 posts
  • LocationColorado, USA

Posted 12 June 2013 - 01:12 AM

View Posttheoriginalbit, on 12 June 2013 - 01:04 AM, said:

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 by happydude11209, 12 June 2013 - 01:19 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users