Advertisement
King0fGamesYami

blink

Jul 18th, 2016
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. --[[
  2. -- SHA-256 implementation in CC-Lua
  3. -- HMAC and PBKDF2 with SHA-256
  4. -- By Anavrins
  5. ]]--
  6.  
  7. if not hash and not os.loadAPI( "hash" ) then
  8.     local handle = http.get( "http://pastebin.com/raw/6UV4qfNF" )
  9.     local file = fs.open( "hash", "w" )
  10.     file.write( handle.readAll() )
  11.     file.close()
  12.     handle.close()
  13.     os.loadAPI( "hash" )
  14. end
  15.  
  16. --[[
  17.     Base64 in Pure Lua
  18. ]]
  19. local enc, dec
  20. -- working lua base64 codec (c) 2006-2008 by Alex Kloss
  21. -- compatible with lua 5.1
  22. -- http://www.it-rfc.de
  23. -- licensed under the terms of the LGPL2
  24.  
  25. -- bitshift functions (<<, >> equivalent)
  26. -- shift left
  27. do
  28.     local function lsh(value,shift)
  29.         return (value*(2^shift)) % 256
  30.     end
  31.  
  32.     -- shift right
  33.     local function rsh(value,shift)
  34.         return math.floor(value/2^shift) % 256
  35.     end
  36.  
  37.     -- return single bit (for OR)
  38.     local function bit(x,b)
  39.         return (x % 2^b - x % 2^(b-1) > 0)
  40.     end
  41.  
  42.     -- logic OR for number values
  43.     local function lor(x,y)
  44.         result = 0
  45.         for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  46.         return result
  47.     end
  48.  
  49.     -- encryption table
  50.     local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  51.  
  52.     -- function encode
  53.     -- encodes input string to base64.
  54.     function enc(data)
  55.         local bytes = {}
  56.         local result = ""
  57.         for spos=0,string.len(data)-1,3 do
  58.             for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  59.             result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  60.         end
  61.         return result
  62.     end
  63.  
  64.     -- decryption table
  65.     local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
  66.  
  67.     -- function decode
  68.     -- decode base64 input to string
  69.     function dec(data)
  70.         local chars = {}
  71.         local result=""
  72.         for dpos=0,string.len(data)-1,4 do
  73.             for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  74.             result = string.format('%s%s%s%s',result,string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and string.char(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
  75.         end
  76.         return result
  77.     end
  78. end
  79.  
  80. --[[
  81. Blink by KingofGamesYami
  82. ]]--
  83.  
  84. local compID = os.getComputerID()
  85.  
  86. local devicePairs, deviceKeys, cache = {}, {}, {}
  87.  
  88. local function getUUID()
  89.     local uuid = math.random( 1, 2147483647 )
  90.     cache[ uuid ] = true
  91.     return uuid
  92. end
  93.  
  94. local function getKey( a, b )
  95.     return enc( hash.hmac( a, b ):toHex() )
  96. end
  97.  
  98. if fs.exists( ".devicepairing" ) then
  99.     local file = fs.open( ".devicepairing", "r" )
  100.     devicePairs = textutils.unserialize( file.readAll() )
  101.     file.close()
  102. end
  103.  
  104. local modem = peripheral.find( "modem", function( name, obj )
  105.     if obj.isWireless() then
  106.         return true
  107.     end
  108. end )
  109.  
  110. if not modem then
  111.     error( "No wireless modem connected!", 2 )
  112. end
  113.  
  114. modem.open( 27 ) -- let's play genocide bingo! (+1 if you get the reference)
  115.  
  116. function pair( deviceID, str )
  117.     devicePairs[ deviceID ] = str
  118.     local file = fs.open( ".devicepairing", "w" )
  119.     file.write( textutils.serialize( devicePairs ) )
  120.     file.close()
  121. end
  122.  
  123. function send( deviceID, message, timeout )
  124.     if not modem.isOpen( 27 ) then
  125.         modem.open( 27 )
  126.     end
  127.     modem.transmit( 27, 27, {blink=true, type="verifyKey", to=deviceID, from = compID, uuid = getUUID()} )
  128.     local str, id
  129.     if timeout then
  130.         id = os.startTimer( timeout )
  131.     end
  132.     while true do
  133.         local event, side, received, reply, message, distance = os.pullEvent()
  134.         if event == "modem_message" and received == 27 and type( message ) == "table" and message.blink and message.type == "keyResponse" and message.from == deviceID then
  135.             str = message.body
  136.             break
  137.         elseif event == "timer" and side == id then
  138.             return false
  139.         end
  140.     end
  141.     modem.transmit( 27, 27, {blink = true, type = "message", to = deviceID, from = compID, body = message, verification = getKey( devicePairs[ compID ], str ), uuid = getUUID() } )
  142.     return true
  143. end
  144.  
  145. function receive( deviceID )
  146.     local event, body, from
  147.     repeat
  148.         event, body, from = os.pullEvent( "blink_message" )
  149.     until from == deviceID
  150.     return from, body
  151. end
  152.  
  153. function setDeviceKey( str )
  154.     return pair( compID, str )
  155. end
  156.  
  157. function run()
  158.     while true do
  159.         local event, side, received, reply, message, distance = os.pullEvent( "modem_message" )
  160.         print( type( message ) == "table" and message.type )
  161.         if received == 27 and type( message ) == "table" and message.blink and message.to == compID and not cache[ message.uuid ] then
  162.             cache[ message.uuid ] = true
  163.             if message.type == "verifyKey" and devicePairs[ message.from ] then
  164.                 local str = hash.digest( tostring{} ):toHex()
  165.                 modem.transmit( 27, 27, {blink = true, type = "keyResponse", from = compID, to = message.from, body = str, uuid = getUUID() } )
  166.                 deviceKeys[ message.from ] = getKey( devicePairs[ message.from ], str )
  167.             elseif message.type == "message" and message.verification == deviceKeys[ message.from ] then
  168.                 os.queueEvent( "blink_message", message.body, message.from )
  169.                 deviceKeys[ message.from ] = nil
  170.             end
  171.         elseif received == 27 and type( message ) == "table" and message.blink and not cache[ message.uuid ] then
  172.             cache[ message.uuid ] = true
  173.             modem.transmit( 27, 27, message )
  174.         end
  175.     end
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement