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

[LUA][Question] Tables

Started by remiX, 23 October 2012 - 02:04 PM
remiX #1
Posted 23 October 2012 - 04:04 PM
I haven't really worked with tables and I'm wondering if something like this would work.


Passwords = {[1] = "pass1", [2] = "derp", [3] = "lol"}

while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Enter password")
  write("  > ")
  checkPassword = read("*")
  if checkPassword = Passwords[1] then
    print("You entered 'pass1'")
    sleep(3)
  elseif checkPassword = Passwords[2] then
    print("You entered 'derp'")
    sleep(3)
  elseif checkPassword = Passwords[3] then
    print("You entered 'lol'")
    sleep(3)
  else
    print("Invalid password: "..checkPassword)
  end
end

If it does, is there an easier way to do it so you don't have to have an elseif for each password?
Kolpa #2
Posted 23 October 2012 - 04:39 PM
pretty much like this but here we also check for usernames

local users = {"User1"}
local passw = {"Pass1"}
local function usercheck(u)
  for x=1,#users do  -- #users is the lenght of the user table
    if users[x] == u then
	  return true, x
    end
  end
return false
end
print("Username:  ")
local user = read()
local check, id = usercheck(user)
if check then
  term.clear()
  term.setCursorPos(1,1)
  print("Password: ")
  local pass = read("*")
  if passw[id] == pass then
    print("Login Sucessfull")
  else
    print("Wrong Password")
  end
else
  print("Wrong Username")
end
ChunLing #3
Posted 23 October 2012 - 05:22 PM
Yes, you just reverse the keys and values in your table:
Passwords = {pass1=1,derp=2,lol=3} 

Then you use a single if then end:
checkPassword = read("*")
if Passwords[checkPassword] then print("You entered "..checkPassword) else print("Invalid password: "..checkPassword) end
Edited on 23 October 2012 - 03:24 PM
Doyle3694 #4
Posted 23 October 2012 - 05:29 PM
the for loop function is very much prefered, doing it like ChunLing is in my opinion the "glitchy" way. for loops are neater in my opinion
ChunLing #5
Posted 23 October 2012 - 05:41 PM
What's glitchy about it? It's super fast and I've used it intensively without ever having any kind of failure.
Doyle3694 #6
Posted 23 October 2012 - 05:43 PM
because you are using variable names as a variable. not much else to say
Lyqyd #7
Posted 23 October 2012 - 05:48 PM
because you are using variable names as a variable. not much else to say

There's nothing technically incorrect or glitchy about ChunLing's method. It has the potential to lead to code that is confusing to people not used to using tables that way, but there's nothing wrong with it. Though, I would use true for their value rather than an index if I were just using it to validate entries without trying to match them to anything, like his example.
Doyle3694 #8
Posted 23 October 2012 - 05:51 PM
No I know there is nothing incorrect or glitchy with it. but it feels very strange.
remiX #9
Posted 23 October 2012 - 06:25 PM
Cool ^_^/>/>

Other than login systems and menu's, what else can you use tables for in lua?
Kolpa #10
Posted 23 October 2012 - 06:55 PM
pretty much anything that stores an unknown length of data or you can do weird encryption tools with it like this:
encrypter: http://pastebin.com/inEL4D1r
decrypter: http://pastebin.com/f0Cywah1
remiX #11
Posted 23 October 2012 - 10:13 PM
pretty much anything that stores an unknown length of data or you can do weird encryption tools with it like this:
encrypter: http://pastebin.com/inEL4D1r
decrypter: http://pastebin.com/f0Cywah1

Does this work 100% for a login system? ^_^/>/>
ChunLing #12
Posted 23 October 2012 - 11:18 PM
You can still use the value under the key, which I assume you'd want if you have three different passwords rather than one.

I guess that you could use the shift encryption on a login…don't know why you would, but you could.
remiX #13
Posted 24 October 2012 - 12:02 AM
You can still use the value under the key, which I assume you'd want if you have three different passwords rather than one.

I guess that you could use the shift encryption on a login…don't know why you would, but you could.

So the host of the server can't check the passwords xD
ChunLing #14
Posted 24 October 2012 - 12:58 AM
Aha…well that particular encryption scheme is pretty weak, you'd want something stronger for persistent data. But yes, you could make it so that the user input an encryption key along with their password, and only the encrypted match would be stored on the server, not the key or original password.

You could also make it so that the first few characters of the password were automatically converted into an encryption key for the password, for ease of use.
Edited on 23 October 2012 - 11:01 PM
Kolpa #15
Posted 24 October 2012 - 01:42 PM
yeah don't use ceaser shift for security its really weak

you could use this function in combination with the http api to create md5 hashes

local function hash(str)
return http.get("http://im-to-poor-to-buy-an-domain.host22.com/md5.php?string="..str).readLine()
end

and save passwords/usernames in that encryption

also using tables is pretty bad since its slow and buggy it would be better to use byte shifting for caesar shift