Advertisement
timmy123456789

Untitled

Aug 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 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.  
  15. local bkey
  16.  
  17. if fs.exists(".bkey") then
  18.   local file = fs.open(".bkey", "r")
  19.   bkey = crypt.decodeKey("RSA", file.readLine())
  20.   pub = crypt.decodeKey("RSA", file.readLine())
  21.   priv = crypt.decodeKey("RSA", file.readLine())
  22.   file.close()
  23. else
  24.   pub, priv = crypt.generateKeyPair("RSA", 1024)
  25. end
  26.  
  27. local function rMsg(enc)
  28.   local time = os.startTimer(1)
  29.   while true do
  30.     local e, p1, p2, p3 = os.pullEvent()
  31.     if e == "rednet_message" then
  32.       if p1 == d and p3 == "luabank" then
  33.         if enc == true then
  34.           return priv.decrypt("RSA", p2)
  35.         else
  36.           return p2
  37.         end
  38.       end
  39.     elseif e == "timer" and p1 == time then
  40.       return nil
  41.     end
  42.   end
  43. end
  44.  
  45. local function sMsg(msg)
  46.   rednet.send(d, "smsgs" .. bkey.encrypt("RSA", msg), "luabank")
  47. end
  48.  
  49. function connect()
  50.   d = rednet.lookup("luabank", "database")
  51.   if not d then
  52.     return false
  53.   elseif not bkey then
  54.     rednet.send(d, "skeys" .. pub.encode(), "luabank")
  55.     local msg = rMsg()
  56.     if msg and msg:sub(1, 5) == "mkeys" then
  57.       bkey = crypt.decodeKey("RSA", msg:sub(6))
  58.       local file = fs.open(".bkey", "w")
  59.       file.writeLine(msg:sub(6))
  60.       file.writeLine(pub.encode())
  61.       file.writeLine(priv.encode())
  62.       file.close()
  63.     else
  64.       return false
  65.     end
  66.     return true
  67.   else
  68.     return true
  69.   end
  70. end
  71.  
  72. function register(user, pass)
  73.   sMsg(textutils.serialize({"r", user, pass}))
  74.   local dat = rMsg(true)
  75.   if not dat then
  76.     return false, "No response"
  77.   elseif dat:sub(1, 5) == "error" then
  78.     return false, dat:sub(6)
  79.   else
  80.     return dat
  81.   end
  82. end
  83.  
  84. function newcard(user, pass)
  85.   sMsg(textutils.serialize({"c", user, pass}))
  86.   local dat = rMsg(true)
  87.   if not dat then
  88.     return false, "No response"
  89.   elseif dat:sub(1, 5) == "error" then
  90.     return false, dat:sub(6)
  91.   else
  92.     return dat
  93.   end
  94. end
  95.  
  96. function transact(card, user, amount)
  97.   sMsg(textutils.serialize({"t", card, user, amount}))
  98.   local dat = rMsg(true)
  99.   if not dat then
  100.     return false, "No response"
  101.   elseif dat:sub(1, 5) == "error" then
  102.     return false, dat:sub(6)
  103.   else
  104.     return dat
  105.   end
  106. end
  107.  
  108. function transactu(user, pass, rec, amount)
  109.   sMsg(textutils.serialize({"tu", user, pass, rec, amount}))
  110.   local dat = rMsg(true)
  111.   if not dat then
  112.     return false, "No response"
  113.   elseif dat:sub(1, 5) == "error" then
  114.     return false, dat:sub(6)
  115.   else
  116.     return dat
  117.   end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement