Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- term.clear()
- term.setCursorPos(1,1)
- return false
- end
- end
- function login()
- term.setTextColor(colors.orange)
- print("Logging in!")
- term.setTextColor(colors.white)
- local a, b = 0, 0
- local t
- local u
- while true do
- term.write("Username: ")
- u = read()
- t = {"/.users/."}
- table.insert(t,u)
- t = table.concat(t)
- print(t)
- if fs.exists(t) == false then
- printError("User doesn't exist")
- a = a + 1
- print("Attempt: "..a)
- os.sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- 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)
- if pw ~= comp then
- printError("Wrong password!")
- os.sleep(2)
- login()
- else
- term.setTextColor(colors.green)
- print("Succesfully logged in!")
- break
- end
- end
- 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)
- print(z)
- if store(x,y,z) then
- term.setTextColor(colors.green)
- print("Succesfully registered")
- break
- end
- end
- login()
- end
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement