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

Security system(Key disk)

Started by Jarle212, 03 November 2012 - 11:13 AM
Jarle212 #1
Posted 03 November 2012 - 12:13 PM
Edit: This is not about a Booting menu btw.
forgot to change the title


Made a small security system using floppy disks as key:


Spoiler

Server:

Server commands:
- genCode



local genCode = "";

local command = {}
local uncode = textutils.unserialize
local code = textutils.serialize

rednet.open("back")







local function genCode()

if not(disk.hasData("bottom")) then
print("Insert disk")
end
while not(disk.hasData("bottom")) do
os.sleep(1)
end

ran = math.random(20000)

genCode = "" .. ran
writeFile("disk/code.gen",{genCode})
writeFile("currentCode.gen",{genCode})
disk.eject("bottom")


end


function setCommands()

command["genCode"] = genCode

end

function getCommand()
print("Type in command:")
local t = read()

if not(command[t] == nil) then
command[t]()
else
print("Do not recognise: " .. t)
end

end


function readFile(file)
if fs.exists(file) then
local filer = io.open(file,"r");
local lines = {}
local str = ""
local count = 0
str = filer:read();
while str do
print("Loading: " .. count)
lines[#lines+1] = str;
count = count + 1
str = filer:read();
end
filer:close()
return lines
else
return "100"
end
end


function writeFile(file,tabel)
local filer = io.open(file,"w");

for i=1, #tabel do
filer:write(tabel[i] .. "\n")
end
filer:close()

end


function input()

event,p1,p2,p3 = os.pullEvent()
if event == "timer" then

os.startTimer(1)
end

if event == "rednet_message" then

redinput(p1,p2)
end
if event == "key" then


end
end


function redinput(id,mess)
local messu = uncode(mess)

if messu[1] == "200" then
if messu[2] == "GETGENCODE" then
if messu[3] == genCode then
rednet.send(id,code({"255",genCode}))
else
rednet.send(id,code({"255","ERROR"}))
end
end
hten end
end



function updateTerminal()
while true do
getCommand()
end
end

function updateInput()
while true do
input()
end
end
setCommands();
print("Loading file...")
local readP = readFile("currentCode.gen")
genCode = readP[1]
os.startTimer(1)
print("Starting...")
parallel.waitForAll(updateInput,updateTerminal)



Client:


local open = true
local timer = 5
local generatedCode = ""
local mainID = 7;
rednet.open("left");
local uncode = textutils.unserialize
local code = textutils.serialize
local runnning = true;
local function send(message)
rednet.send(mainID,code(message))
end

local function getgeneratedCode(try)
print("Sending data...")
send({"200","GETGENCODE",try})
local umess = {};
umess[1] = ""
umess[2] = " "
print("Receiveing data...")
i , mess = rednet.receive(2);
while (i ~= mainID) do
if (mess == nil) then
break
end
print("Receiveing data...")
local i , mess rednet.receive(2);
end

if (mess ~= nil) then
umess = uncode(mess)
return umess[2]
else
return "ERROR"
end


end
local function readDisk()
local  file = io.open("disk/code.gen","r")
local line = {}
redln = file:read();
if (redln == getgeneratedCode(redln)) and (redln ~= "ERROR") then
opent()
end
file:close()
end
function opent()
rs.setOutput("right",true)
disk.eject("back")
os.sleep(timer)
rs.setOutput("right",false)
end

while runnning do
term.clear()
print("Insert disk")
os.sleep(1)
if disk.hasData("back") then
if fs.exists("disk/code.gen")then
print("Reading disk..")
readDisk()
end
end
end



rick3333 #2
Posted 01 January 2013 - 07:02 PM
there is an error at line 104 in the genCode script
Dlcruz129 #3
Posted 01 January 2013 - 07:25 PM
Very, very nice for a first program! (I'm assuming this is one of your first programs based on your post count, forgive me if I'm wrong)
Jarle212 #4
Posted 27 January 2013 - 11:12 PM
Think the error is fixed now

Very, very nice for a first program! (I'm assuming this is one of your first programs based on your post count, forgive me if I'm wrong)
Not my first :P/>
Not posting too much.