Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function rKey()
- local key = math.randomseed(os.time() * (10^9))
- key = math.random(0,9999999999)
- key = string.format("%x", key)
- return key
- end
- function hash(pw, key)
- local pw = pw
- local key = key
- local ascii = {}
- local ascii_2 = {}
- for i = 1, #key do
- ascii[i] = string.byte(key, i)
- end
- for j = 1, #pw do
- ascii_2[j] = string.byte(pw, j)
- end
- local n = string.reverse(table.concat(ascii))
- n = tonumber(n)
- local m = string.reverse(table.concat(ascii_2))
- m = tonumber(m)
- local key_n = math.randomseed(n)
- key_n = math.random(0,9999999999)
- local key_m = math.randomseed(m)
- key_m = math.random(0,9999999999)
- local mod_n = key_n % #key
- local mod_m = key_m % #pw
- local xor = bit.bxor(key_n, key_m)
- local hex = string.format("%x", xor * (mod_n+mod_m+1))
- return hex
- end
- function store(user, key, pw)
- local user = user
- local key = key
- local pw = pw
- if fs.exists("/.users") == false then
- fs.makeDir("/.users")
- end
- local t = {"/.users/"}
- table.insert(t, user)
- t = table.concat(t)
- if fs.exists(t) == false then
- local x = fs.open(t, "w")
- x.writeLine(key)
- x.writeLine(pw)
- x.close()
- return true
- else
- printError("Username already taken")
- sleep(1)
- clear()
- return false
- end
- end
- function login()
- local a, b = 0, 0
- local t
- local u
- while true do
- term.setTextColor(colors.orange)
- print("Logging in")
- term.setTextColor(colors.white)
- term.write("Username: ")
- u = read()
- t = {"/.users/"}
- table.insert(t,u)
- t = table.concat(t)
- if fs.exists(t) == false then
- printError("User doesn't exist")
- a = a + 1
- print("Attempt: "..a)
- os.sleep(2)
- clear()
- if a >= 3 then
- printError("Shutting down")
- os.sleep(2)
- os.shutdown()
- end
- else
- break
- end
- end
- while true do
- local file = fs.open(t, "r")
- term.write("Password: ")
- local p = read('*')
- local nKey = file.readLine()
- local key = nKey
- nKey = file.readLine()
- local pw = nKey
- local comp = hash(p, key)
- file.close()
- if pw ~= comp then
- printError("Wrong password!")
- os.reboot()
- else
- term.setTextColor(colors.green)
- print("Succesfully logged in!")
- term.setTextColor(colors.white)
- os.sleep(2)
- clear()
- break
- end
- end
- return t
- end
- function register()
- while true do
- term.setTextColor(colors.orange)
- print("Registering!")
- term.setTextColor(colors.white)
- term.write("Username: ")
- local x = read()
- local y = rKey()
- term.write("Password: ")
- local z = read('*')
- local z = hash(z, y)
- if store(x,y,z) then
- term.setTextColor(colors.green)
- print("Succesfully registered")
- term.setTextColor(colors.white)
- os.sleep(2)
- clear()
- break
- end
- end
- login()
- end
- function deleteUser()
- user = login()
- term.setTextColor(colors.orange)
- print("Deleting account")
- term.setTextColor(colors.white)
- print("Are you sure? [y/n]")
- while true do
- event, key = os.pullEvent("key")
- if key == keys.y then
- fs.delete(user)
- term.setTextColor(colors.green)
- print("Deleted user")
- term.setTextColor(colors.white)
- os.sleep(2)
- clear()
- break
- elseif key == keys.n then
- term.setTextColor(colors.green)
- print("Cancelled deleting")
- term.setTextColor(colors.white)
- os.sleep(2)
- clear()
- end
- end
- selector()
- end
- function selector()
- local selector = 1
- term.setTextColor(colors.blue)
- print("[Login]")
- term.setTextColor(colors.white)
- print("Register")
- print("Delete user")
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.down and selector < 3 then
- selector = selector + 1
- elseif key == keys.up and selector > 1 then
- selector = selector - 1
- end
- if selector == 1 then
- clear()
- term.setTextColor(colors.blue)
- print("[Login]")
- term.setTextColor(colors.white)
- print("Register")
- print("Delete user")
- elseif selector == 2 then
- clear()
- term.setTextColor(colors.white)
- print("Login")
- term.setTextColor(colors.blue)
- print("[Register]")
- term.setTextColor(colors.white)
- print("Delete user")
- elseif selector == 3 then
- clear()
- term.setTextColor(colors.white)
- print("Login")
- print("Register")
- term.setTextColor(colors.blue)
- print("[Delete user]")
- end
- if key == keys.enter and selector == 1 then
- clear()
- login()
- break
- elseif key == keys.enter and selector == 2 then
- clear()
- register()
- break
- elseif key == keys.enter and selector == 3 then
- clear()
- deleteUser()
- break
- end
- end
- end
- selector()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement