Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local BANK_CHANNEL = 1329
- local function convert(chars,dist,inv)
- local charInt = string.byte(chars);
- for i=1,dist do
- if(inv)then charInt = charInt - 1; else charInt = charInt + 1; end
- if(charInt<32)then
- if(inv)then charInt = 126; else charInt = 126; end
- elseif(charInt>126)then
- if(inv)then charInt = 32; else charInt = 32; end
- end
- end
- return string.char(charInt);
- end
- local function crypt(str,k,inv)
- local enc= "";
- for i=1,#str do
- if(#str-k[5] >= i or not inv)then
- for inc=0,3 do
- if(i%4 == inc)then
- enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
- break;
- end
- end
- end
- end
- if(not inv)then
- for i=1,k[5] do
- enc = enc .. string.char(math.random(32,126));
- end
- end
- return enc;
- end
- local modem
- local function getModem()
- for i,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modem = peripheral.wrap(v)
- modem.open(BANK_CHANNEL)
- break
- end
- end
- end
- local keys = {}
- local logs = {}
- local users = {}
- local function loadLogs()
- if fs.exists("logs") then
- local file = fs.open("log", "r")
- logs = textutils.unserialize(file.readAll())
- file.close()
- end
- end
- local function loadUsers()
- if fs.exists("users") then
- local file = fs.open("users", "r")
- users = textutils.unserialize(file.readAll())
- file.close()
- end
- end
- local function saveUsers()
- local file = fs.open("users", "w")
- file.write(textutils.serialize(users))
- file.close()
- end
- local function saveLogs()
- local file = fs.open("log", "w")
- file.write(textutils.serialize(logs))
- file.close()
- end
- local function log(text)
- table.insert(logs, text)
- saveLogs()
- print(text)
- end
- local function generateKey()
- return {math.random(1, 255), math.random(1, 255), math.random(1, 255), math.random(1, 255), math.random(1, 255)}
- end
- local function receiveData()
- local event, side, from, id, data
- while true do
- event, side, from, id, data = os.pullEvent("modem_message")
- if from == BANK_CHANNEL then
- if data == "@!)#njsdf)(#84njd)" then -- Renew key
- keys[id] = generateKey()
- modem.transmit(BANK_CHANNEL, BANK_CHANNEL, textutils.serialize(keys[id]));
- elseif keys[id] then
- data = crypt(data, keys[id], true)
- print(data)
- data = textutils.unserialize(data)
- if type(data) == "table" then
- return id, data
- end
- end
- end
- end
- end
- local function sendMessage(text, id)
- modem.transmit(BANK_CHANNEL, BANK_CHANNEL, crypt(textutils.serialize({id, text}), myKey))
- end
- local function main() -- The main function
- getModem()
- loadLogs()
- loadUsers()
- if modem == nil then
- log("No modem attached")
- return
- end
- while true do
- pcall(function()
- local id, data = receiveData()
- if data[1] == "createUser" then
- if data[2] and data[3] then
- if not users[data[2]:lower()] then
- users[data[2]:lower()] = {data[3], 0}
- sendMessage({true}, id)
- log("Created user '" .. data[2] .. "'")
- else
- sendMessage({false, "User exists"}, id)
- end
- else
- sendMessage({false, "Username or password empty"})
- end
- end
- end)
- end
- end
- main()
Add Comment
Please, Sign In to add comment