Advertisement
theinsekt

doorlock2

Sep 9th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. --theinsekt's doorlock program
  2. --heavily influenced by http://computercraft.info/wiki/Making_a_Password_Protected_Door
  3. --uses sha256-implementation from http://pastebin.com/gsFrNjbt  (GravityScore)
  4.  
  5. --to install:
  6. --in computercraft:
  7. --do: rm getlock
  8. --do: pastebin get XCG38Zpi getlock
  9. --do: getlock
  10. --finally hold ctrl-r
  11.  
  12. os.loadAPI("sha256")
  13.  
  14. function hash(msg)
  15.  return sha256.sha256(msg)
  16. end
  17.  
  18. local passPath="password"
  19. local adminPath="adminpass"
  20. local sidePath="side"
  21. local doorTime=3
  22. local testing=false
  23.  
  24.  
  25.  
  26.  
  27.  
  28. function loadValue(path)
  29.  local succes=true
  30.  if  not fs.exists(path) then
  31.   return nil
  32.  end
  33.  local h=fs.open(path, "r")
  34.  if h==nil then
  35.   return nil
  36.  end
  37.  local value=h.readAll()
  38.  h.close()
  39.  return value
  40. end
  41.  
  42.  
  43.  
  44. function saveValue(value,path)
  45.  if type(value)~="string" then
  46.   error("Error: value must be a string")
  47.  end
  48.  local h = fs.open(path, "w")
  49.  h.write(value)
  50.  h.close()
  51.  return true
  52. end
  53.  
  54. --function to set to enable or disable ctrl-t
  55. --http://www.computercraft.info/forums2/index.php?/topic/2732-disable-ctrlt/
  56. local pullEventCopy = os.pullEvent
  57. function setCtrlT(value)
  58.  if value then
  59.   os.pullEvent = pullEventCopy
  60.  else
  61.   os.pullEvent = os.pullEventRaw
  62.  end
  63. end
  64.  
  65.  
  66.  
  67. function checkConfiguration()--checkConfiguration()
  68.  return loadValue(passPath)~=nil and loadValue(adminPath)~=nil and loadValue(sidePath)~=nil
  69. end
  70.  
  71.  
  72. function loadDoorValues()
  73.  return loadValue(passPath), loadValue(sidePath), loadValue(adminPath)
  74. end
  75.  
  76. function adminMode()
  77.  print("Admin mode.")
  78.  setCtrlT(true)--enable ctrl-t
  79.  print("Hold ctrl-t for root access.")
  80.  print("Hold ctrl-r to exit")
  81.  
  82.  --get a password
  83.  print("New door password:")
  84.  local password=read("*")
  85.  
  86.  -- get a side
  87.  local sides=redstone.getSides()
  88.  local side=nil
  89.  while(true) do
  90.   for k,v in ipairs(sides) do
  91.    print(k,":",v)
  92.   end
  93.   print("New door side (number):")
  94.   side=read("*")
  95.   if not sides[tonumber(side)] then
  96.    print("Invalid side")
  97.   else
  98.    side=sides[tonumber(side)]
  99.    break
  100.   end
  101.  end
  102.  
  103. --get an admin password
  104.  local adminPass=nil
  105.  while(true) do
  106.   print("New admin password:")
  107.   local adminPass1=read("*")
  108.   print("New admin password again:")
  109.   local adminPass2=read("*")
  110.   if adminPass1~=adminPass2 then
  111.    print("They are not identical")
  112.   else
  113.    adminPass=adminPass1
  114.    break
  115.   end
  116.  end
  117.  
  118.  print("Saving...")
  119.  saveValue(hash(password),passPath)--hash passwords
  120.  saveValue(side,sidePath)
  121.  saveValue(hash(adminPass),adminPath)--hash passwords
  122.  
  123.  
  124.  --sleep(2) --hashing will take time, so don't need sleep
  125.  setCtrlT(false)--disable ctrl-t
  126. end
  127.  
  128. function clear()
  129.  term.clear()
  130.  term.setCursorPos(1, 1)
  131. end
  132.  
  133. function printProgramInfo()
  134.  print("theinsekt's lock v2")
  135.  print("to install:")
  136.  print("do: rm getlock")
  137.  print("do: pastebin get XCG38Zpi getlock")
  138.  print("do: getlock")
  139.  print("finally hold ctrl-r")
  140.  print()
  141. end
  142.  
  143. setCtrlT(false)--disable ctrl-t
  144. clear()
  145. print("booting...")
  146. sleep(1)
  147. while(true) do
  148.  clear()
  149.  if testing then
  150.   print("debug mode, reactivating ctrl-t")
  151.   setCtrlT(true)--enable ctrl-t
  152.  end
  153.  printProgramInfo()
  154.  if not checkConfiguration() then
  155.   adminMode()
  156.  else --is configured so check password
  157.   local password,side,adminPass=loadDoorValues()
  158.   --print(password, ":", side, ":",adminPass)
  159.   print("Restricted mode.")
  160.   print("Password:")
  161.   local input = read("*")
  162.   input=hash(input)--because passwords are stored hashed
  163.   if input==adminPass then --admin
  164.    clear()
  165.    printProgramInfo()
  166.    adminMode()
  167.   elseif input == password then --password
  168.    print("Correct!")
  169.    redstone.setOutput(side, true)
  170.    sleep(doorTime)
  171.    redstone.setOutput(side, false)
  172.   else --fail
  173.    print("Incorrect!")
  174.    sleep(2)
  175.   end
  176.  end
  177.  
  178. end
  179.  
  180. setCtrlT(true)--enable ctrl-t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement