D_Puppy

Door system for oc and opensecurity

Jul 22nd, 2016
3,433
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local accessLevel = 2
  2. local cryptKey = {1, 2, 3, 4, 5}
  3. local modemPort = 199
  4.  
  5.  
  6.  
  7. local component = require("component")
  8. local gpu = component.gpu
  9. local event = require("event")
  10. local ser = require("serialization")
  11. local term = require("term")
  12. local computer = component.computer
  13.  
  14. local door = component.os_door
  15. local magReader = component.os_magreader
  16.  
  17. local modem = component.modem
  18.  
  19.  
  20. local function convert( chars, dist, inv )
  21.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  22. end
  23.  
  24. local function crypt(str,k,inv)
  25.   local enc= "";
  26.   for i=1,#str do
  27.     if(#str-k[5] >= i or not inv)then
  28.       for inc=0,3 do
  29.     if(i%4 == inc)then
  30.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  31.       break;
  32.     end
  33.       end
  34.     end
  35.   end
  36.   if(not inv)then
  37.     for i=1,k[5] do
  38.       enc = enc .. string.char(math.random(32,126));
  39.     end
  40.   end
  41.   return enc;
  42. end
  43.  
  44.  
  45. function splitString(str, sep)
  46.         local sep, fields = sep or ":", {}
  47.         local pattern = string.format("([^%s]+)", sep)
  48.         str:gsub(pattern, function(c) fields[#fields+1] = c end)
  49.         return fields
  50. end
  51.  
  52. term.clear()
  53. print("Security door   Access level : " .. tostring(accessLevel))
  54. print("---------------------------------------------------------------------------")
  55.  
  56. if modem.isOpen(modemPort) == false then
  57.   modem.open(modemPort)
  58. end
  59. data = crypt(tostring(accessLevel), cryptKey)
  60. modem.broadcast(modemPort, "setlevel", data)
  61.  
  62. while true do
  63.   ev, _, user, str, uuid = event.pull("magData")
  64.   local data = crypt(str, cryptKey, true)
  65.   if ev then
  66.     local tmpTable = splitString(data," ")
  67.     term.write(tmpTable[3] .. ":")
  68.     if modem.isOpen(modemPort) == false then
  69.       modem.open(modemPort)
  70.     end
  71.     data = crypt(tmpTable[3], cryptKey)
  72.     modem.broadcast(modemPort, "checkuser", data)
  73.     local e, _, from, port, _, msg = event.pull(1, "modem_message")
  74.     if e then
  75.       data = crypt(msg, cryptKey, true)
  76. --    print(data)
  77.       if data == "true" then
  78.     term.write("Access granted\n")
  79.     computer.beep()
  80.     door.toggle()
  81.     os.sleep(2)
  82.     door.toggle()
  83.       else
  84.     term.write("Access denied\n")
  85.       end
  86.     else
  87.       term.write("server timeout\n")
  88.     end
  89.   end
  90. end
RAW Paste Data