Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --theinsekt's doorlock program
- --heavily influenced by http://computercraft.info/wiki/Making_a_Password_Protected_Door
- --uses sha256-implementation from http://pastebin.com/gsFrNjbt (GravityScore)
- --to install:
- --in computercraft:
- --do: rm getlock
- --do: pastebin get XCG38Zpi getlock
- --do: getlock
- --finally hold ctrl-r
- os.loadAPI("sha256")
- function hash(msg)
- return sha256.sha256(msg)
- end
- local passPath="password"
- local adminPath="adminpass"
- local sidePath="side"
- local doorTime=3
- local testing=false
- function loadValue(path)
- local succes=true
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local value=h.readAll()
- h.close()
- return value
- end
- function saveValue(value,path)
- if type(value)~="string" then
- error("Error: value must be a string")
- end
- local h = fs.open(path, "w")
- h.write(value)
- h.close()
- return true
- end
- --function to set to enable or disable ctrl-t
- --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
- local pullEventCopy = os.pullEvent
- function setCtrlT(value)
- if value then
- os.pullEvent = pullEventCopy
- else
- os.pullEvent = os.pullEventRaw
- end
- end
- function checkConfiguration()--checkConfiguration()
- return loadValue(passPath)~=nil and loadValue(adminPath)~=nil and loadValue(sidePath)~=nil
- end
- function loadDoorValues()
- return loadValue(passPath), loadValue(sidePath), loadValue(adminPath)
- end
- function adminMode()
- print("Admin mode.")
- setCtrlT(true)--enable ctrl-t
- print("Hold ctrl-t for root access.")
- print("Hold ctrl-r to exit")
- --get a password
- print("New door password:")
- local password=read("*")
- -- get a side
- local sides=redstone.getSides()
- local side=nil
- while(true) do
- for k,v in ipairs(sides) do
- print(k,":",v)
- end
- print("New door side (number):")
- side=read("*")
- if not sides[tonumber(side)] then
- print("Invalid side")
- else
- side=sides[tonumber(side)]
- break
- end
- end
- --get an admin password
- local adminPass=nil
- while(true) do
- print("New admin password:")
- local adminPass1=read("*")
- print("New admin password again:")
- local adminPass2=read("*")
- if adminPass1~=adminPass2 then
- print("They are not identical")
- else
- adminPass=adminPass1
- break
- end
- end
- print("Saving...")
- saveValue(hash(password),passPath)--hash passwords
- saveValue(side,sidePath)
- saveValue(hash(adminPass),adminPath)--hash passwords
- --sleep(2) --hashing will take time, so don't need sleep
- setCtrlT(false)--disable ctrl-t
- end
- function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- function printProgramInfo()
- print("theinsekt's lock v2")
- print("to install:")
- print("do: rm getlock")
- print("do: pastebin get XCG38Zpi getlock")
- print("do: getlock")
- print("finally hold ctrl-r")
- print()
- end
- setCtrlT(false)--disable ctrl-t
- clear()
- print("booting...")
- sleep(1)
- while(true) do
- clear()
- if testing then
- print("debug mode, reactivating ctrl-t")
- setCtrlT(true)--enable ctrl-t
- end
- printProgramInfo()
- if not checkConfiguration() then
- adminMode()
- else --is configured so check password
- local password,side,adminPass=loadDoorValues()
- --print(password, ":", side, ":",adminPass)
- print("Restricted mode.")
- print("Password:")
- local input = read("*")
- input=hash(input)--because passwords are stored hashed
- if input==adminPass then --admin
- clear()
- printProgramInfo()
- adminMode()
- elseif input == password then --password
- print("Correct!")
- redstone.setOutput(side, true)
- sleep(doorTime)
- redstone.setOutput(side, false)
- else --fail
- print("Incorrect!")
- sleep(2)
- end
- end
- end
- setCtrlT(true)--enable ctrl-t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement