Advertisement
King0fGamesYami

Link

Sep 24th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. --[[
  2. link is a secure device-pairing system that can be used in a multitude of ways
  3. ]]--
  4.  
  5. local links
  6.  
  7. local function genNew()
  8.     local t = ""
  9.     for i= 1, 15 do
  10.         t = t .. string.char( math.random( 0, 255 ) )
  11.     end
  12.     return t
  13. end
  14.  
  15. local function increment( secret, current )
  16.     local new = ""
  17.     for i = 1, 15 do
  18.         local n = tostring( secret:sub( i, i ):byte()^math.min( current:sub( i, i ):byte(), 128 ) )
  19.         print( n )
  20.         if #n < 5 then
  21.             new = new .. string.char( tonumber( n:sub( 1, 2 ) ) )
  22.         else
  23.             local result = math.floor( tonumber( n:sub( 1, 2 ) ) / 99/math.ceil( #n - 2 ) )
  24.             new = new .. n:sub( result * 2 + 1, result * 2 + 2 )
  25.         end
  26.     end
  27.     return new
  28. end
  29.  
  30. local function save() --saves the links
  31.     local file = fs.open( ".links", "w" )
  32.     file.write( textutils.serialize( links ) )
  33.     file.close()
  34. end
  35.  
  36. --loading linkages
  37. do
  38.     local file = fs.open( ".links", "r" )
  39.     if file then
  40.         local data = file.readAll()
  41.         file.close()
  42.         links = textutils.unserialize( data )
  43.     else
  44.         links = {}
  45.         links.__secret = ""
  46.         for i = 1, 15 do --generate a secret key
  47.             links.__secret = links.__secret .. string.char( math.random( 31, 127 ) )
  48.         end
  49.         save()
  50.     end
  51. end
  52.  
  53. function addLink( devicename, secret )
  54.     links[ devicename ] = {secret = secret, current=genNew()}
  55. end
  56.  
  57. function getSecret()
  58.     return links.__secret
  59. end
  60.  
  61. function setDeviceName( name )
  62.     links.__deviceName = name
  63. end
  64.  
  65. local mt = {
  66.     send = function( self, message )
  67.         local auth = increment( links[ self.deviceName ].secret, links[ self.deviceName ].current )
  68.         rednet.broadcast( textutils.serialize( {msg=message, auth=auth} ) )
  69.         local id = os.startTimer( 0.05 )
  70.         while true do
  71.             local event = {os.pullEvent()}
  72.             if event[ 1 ] == "rednet_message" and event[ 2 ] == "_confirm_" then
  73.                 links[ self.deviceName ].current = auth
  74.                 save()
  75.                 break
  76.             elseif event[ 1 ] == "timer" and event[ 2 ] == id then
  77.                 break
  78.             end
  79.         end
  80.     end,
  81.    
  82.     event = function( self, tEventData )
  83.         local e = tEventData or {os.pullEvent()}
  84.         if e[ 1 ] == "rednet_message" then
  85.             local msg = textutils.unserialize( e[ 3 ] )
  86.             local new = increment( links.__secret, links[ self.deviceName ].current )
  87.             if type( msg ) == "table" and msg.auth == new then
  88.                 links[ self.deviceName ].current = new
  89.                 save()
  90.                 rednet.send( e[2], "_confirm_" )
  91.                 return "link_message", self.deviceName, msg
  92.             end
  93.         end
  94.         return e
  95.     end,
  96. }
  97.  
  98. function establishLink( deviceName )
  99.     return setmetatable( {deviceName = deviceName}, {__index = mt} )
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement