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

Variables in tables

Started by DiamondTNT, 08 June 2014 - 12:58 AM
DiamondTNT #1
Posted 08 June 2014 - 02:58 AM
I have a table of variable names and variables already set, such as:

hash_startup="43ff582a012f27323c5249e64e28291a077ffd7949a074f2a80b5867ffdbe3f7"
hash_fileexplorer="5670803cddda0e5a34496cf33a0b21782fa80ea653a472fdee67d421cb0060c2"
hash_deletefile="b99fd5065b9f95a4487f25d1522f8d85ca24a87e7f71523a6f9ce1922cf914ac"
local randomTable={"startup",
"fileexplorer",
"deletefile"}
Assuming I couldn't just do "local newVar1=hash_startup", is there anyway to take the value of "randomTable[1]" and convert it to a variable? If you didn't understand the previous sentence, suppose I was trying to have a table with 25+ strings inside and wanted to compare the hash of startup and the value in hash_startup, how should I revise this?

for i=1, #randomTable do
	local fileRead=fs.open(randomTable[i], "r")
	local hash=sha256(fileRead.readAll())
	fileRead.close()
	if hash~="hash_"..randomTable[i] then
              print("HASH DOES NOT MATCH")
	end
end
Edited on 08 June 2014 - 12:59 AM
theoriginalbit #2
Posted 08 June 2014 - 03:17 AM
Firstly I'd like to say that based off what it seems you're trying to do you'd be better off using a CRC (KillaVanilla has one iirc) as its faster and enough for checking file integrity.

Now as for your problem, you'd probably be best to use a key/value paired table like so (example below use SHA256, but I still recommend CRC)

local hashValues = {
  ["startup"] = "43ff582a012f27323c5249e64e28291a077ffd7949a074f2a80b5867ffdbe3f7",
  ["fileexplorer"] = "5670803cddda0e5a34496cf33a0b21782fa80ea653a472fdee67d421cb0060c2",
  --# etc
}

for file, hash in pairs( hashValues ) do
  local f = fs.open( file, 'r' )
  local h = sha256( f.readAll() )
  f.close()
  if h ~= hash then
    print( "File " .. file .. " has been modified" )
  end
end

It should be noted that if you have these values in your program it means that someone could easily modify your hash values, and/or just remove the code that verifies the integrity of your programs.
DiamondTNT #3
Posted 08 June 2014 - 03:28 AM
Thanks.