Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local crypt
- for i,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- rednet.open(v)
- elseif peripheral.getType(v) == "cryptographic accelerator" then
- crypt = peripheral.wrap(v)
- end
- end
- local d
- local pub, priv
- local bkey
- local akey
- if fs.exists(".akey") then
- local file = fs.open(".akey", "r")
- akey = crypt.decodeKey("AES", file.readLine())
- bkey = crypt.decodeKey("RSA", file.readLine())
- pub = crypt.decodeKey("RSA", file.readLine())
- priv = crypt.decodeKey("RSA", file.readLine())
- file.close()
- else
- pub, priv = crypt.generateKeyPair("RSA", 1024)
- end
- local function rMsg(enc)
- local time = os.startTimer(1)
- while true do
- local e, p1, p2, p3 = os.pullEvent()
- if e == "rednet_message" then
- if p1 == d and p3 == "luabank" then
- if enc == true then
- return akey.decrypt("AES/CFB/NoPadding", p2)
- else
- return p2
- end
- end
- elseif e == "timer" and p1 == time then
- return nil
- end
- end
- end
- local function sMsg(msg)
- rednet.send(d, "smsgs" .. akey.encrypt("AES/CFB/NoPadding", msg), "luabank")
- end
- function connect()
- d = rednet.lookup("luabank", "database")
- if not d then
- return false
- elseif not bkey then
- rednet.send(d, "skeys" .. pub.encode(), "luabank")
- local msg = rMsg()
- if msg and msg:sub(1, 5) == "mkeys" then
- local tub = msg:sub(6)
- bkey = crypt.decodeKey("RSA", tub)
- rednet.send(d, "tkeys" .. bkey.encrypt("RSA", "start"), "luabank")
- local dut = rMsg()
- if not dut then
- return false
- end
- if dut:sub(1, 5) == "ykeys" then
- akey = crypt.decodeKey("AES", dut:sub(6))
- local file = fs.open(".akey", "w")
- file.writeLine(dut:sub(6))
- file.writeLine(tub)
- file.writeLine(pub.encode())
- file.writeLine(priv.encode())
- file.close()
- else
- return false
- end
- else
- return false
- end
- return true
- else
- return true
- end
- end
- function register(user, pass)
- sMsg(textutils.serialize({"r", user, pass}))
- local dat = rMsg(true)
- if not dat then
- return false, "No response"
- elseif dat:sub(1, 5) == "error" then
- return false, dat:sub(6)
- else
- return dat
- end
- end
- function newcard(user, pass)
- sMsg(textutils.serialize({"c", user, pass}))
- local dat = rMsg(true)
- if not dat then
- return false, "No response"
- elseif dat:sub(1, 5) == "error" then
- return false, dat:sub(6)
- else
- return dat
- end
- end
- function transact(card, user, amount)
- sMsg(textutils.serialize({"t", card, user, amount}))
- local dat = rMsg(true)
- if not dat then
- return false, "No response"
- elseif dat:sub(1, 5) == "error" then
- return false, dat:sub(6)
- else
- return dat
- end
- end
- function transactu(user, pass, rec, amount)
- sMsg(textutils.serialize({"tu", user, pass, rec, amount}))
- local dat = rMsg(true)
- if not dat then
- return false, "No response"
- elseif dat:sub(1, 5) == "error" then
- return false, dat:sub(6)
- else
- return dat
- end
- end
- function transactl(user, pass, rec, amount)
- sMsg(textutils.serialize({"tl", user, pass, rec, amount}))
- local dat = rMsg(true)
- if not dat then
- return false, "No response"
- elseif dat:sub(1, 5) == "error" then
- return false, dat:sub(6)
- else
- return dat
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement