Advertisement
Alakazard12

bapi

Aug 2nd, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local crypt
  2.  
  3. for i,v in pairs(peripheral.getNames()) do
  4.     if peripheral.getType(v) == "modem" then
  5.         rednet.open(v)
  6.     elseif peripheral.getType(v) == "cryptographic accelerator" then
  7.         crypt = peripheral.wrap(v)
  8.     end
  9. end
  10.  
  11. local d
  12.  
  13. local pub, priv
  14. local bkey
  15. local akey
  16.  
  17. if fs.exists(".akey") then
  18.     local file = fs.open(".akey", "r")
  19.     akey = crypt.decodeKey("AES", file.readLine())
  20.     bkey = crypt.decodeKey("RSA", file.readLine())
  21.     pub = crypt.decodeKey("RSA", file.readLine())
  22.     priv = crypt.decodeKey("RSA", file.readLine())
  23.     file.close()
  24. else
  25.     pub, priv = crypt.generateKeyPair("RSA", 1024)
  26. end
  27.  
  28. local function rMsg(enc)
  29.     local time = os.startTimer(1)
  30.     while true do
  31.         local e, p1, p2, p3 = os.pullEvent()
  32.         if e == "rednet_message" then
  33.             if p1 == d and p3 == "luabank" then
  34.                 if enc == true then
  35.                     return akey.decrypt("AES/CFB/NoPadding", p2)
  36.                 else
  37.                     return p2
  38.                 end
  39.             end
  40.         elseif e == "timer" and p1 == time then
  41.             return nil
  42.         end
  43.     end
  44. end
  45.  
  46. local function sMsg(msg)
  47.     rednet.send(d, "smsgs" .. akey.encrypt("AES/CFB/NoPadding", msg), "luabank")
  48. end
  49.  
  50. function connect()
  51.     d = rednet.lookup("luabank", "database")
  52.     if not d then
  53.         return false
  54.     elseif not bkey then
  55.         rednet.send(d, "skeys" .. pub.encode(), "luabank")
  56.         local msg = rMsg()
  57.         if msg and msg:sub(1, 5) == "mkeys" then
  58.             local tub = msg:sub(6)
  59.             bkey = crypt.decodeKey("RSA", tub)
  60.  
  61.             rednet.send(d, "tkeys" .. bkey.encrypt("RSA", "start"), "luabank")
  62.             local dut = rMsg()
  63.             if not dut then
  64.                 return false
  65.             end
  66.             if dut:sub(1, 5) == "ykeys" then
  67.                 akey = crypt.decodeKey("AES", dut:sub(6))
  68.                 local file = fs.open(".akey", "w")
  69.                 file.writeLine(dut:sub(6))
  70.                 file.writeLine(tub)
  71.                 file.writeLine(pub.encode())
  72.                 file.writeLine(priv.encode())
  73.                 file.close()
  74.             else
  75.                 return false
  76.             end
  77.         else
  78.             return false
  79.         end
  80.         return true
  81.     else
  82.         return true
  83.     end
  84. end
  85.  
  86. function register(user, pass)
  87.     sMsg(textutils.serialize({"r", user, pass}))
  88.     local dat = rMsg(true)
  89.     if not dat then
  90.         return false, "No response"
  91.     elseif dat:sub(1, 5) == "error" then
  92.         return false, dat:sub(6)
  93.     else
  94.         return dat
  95.     end
  96. end
  97.  
  98. function newcard(user, pass)
  99.     sMsg(textutils.serialize({"c", user, pass}))
  100.     local dat = rMsg(true)
  101.     if not dat then
  102.         return false, "No response"
  103.     elseif dat:sub(1, 5) == "error" then
  104.         return false, dat:sub(6)
  105.     else
  106.         return dat
  107.     end
  108. end
  109.  
  110. function transact(card, user, amount)
  111.     sMsg(textutils.serialize({"t", card, user, amount}))
  112.     local dat = rMsg(true)
  113.     if not dat then
  114.         return false, "No response"
  115.     elseif dat:sub(1, 5) == "error" then
  116.         return false, dat:sub(6)
  117.     else
  118.         return dat
  119.     end
  120. end
  121.  
  122. function transactu(user, pass, rec, amount)
  123.     sMsg(textutils.serialize({"tu", user, pass, rec, amount}))
  124.     local dat = rMsg(true)
  125.     if not dat then
  126.         return false, "No response"
  127.     elseif dat:sub(1, 5) == "error" then
  128.         return false, dat:sub(6)
  129.     else
  130.         return dat
  131.     end
  132. end
  133.  
  134. function transactl(user, pass, rec, amount)
  135.     sMsg(textutils.serialize({"tl", user, pass, rec, amount}))
  136.     local dat = rMsg(true)
  137.     if not dat then
  138.         return false, "No response"
  139.     elseif dat:sub(1, 5) == "error" then
  140.         return false, dat:sub(6)
  141.     else
  142.         return dat
  143.     end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement