-- No term for you :)
Original_pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
settings.set("shell.allow_disk_startup", false) -- Prevent disk startup bypass
-- Header stuff
local hostname = os.getComputerLabel() or "craftos"
term.clear()
term.setCursorPos(1,1)
write(_G._ENV._HOST .. " " .. hostname)

-- Load crypto library (slow, so we do it after everything else)
Crypto = require("ccryptolib")
Crypto.random.initWithTiming()

function checkCreds(username, hashed)

	if Crypto.util.fromHex(settings.get("passwd")[username]) == hashed then
		return true
	else
		return false
	end
end

function login()
	print()
	write(hostname .. " login: ")
	local username = read()
	write("Password: ")
	local passwordHash = Crypto.sha512.digest(read("")) -- Hash immidiately so password is not stored plaintext in memory
	if checkCreds(username, passwordHash) then
		print("Welcome ".. username)
		_G.loggedInUser = username
	else
		print("Login incorrect")
		login()
	end
end

if not settings.get("passwd") then
	print()
	print("WARNING: passwd file does not exist, generating default login root:toor")
	settings.set("passwd", { ["root"] = Crypto.util.toHex(Crypto.sha512.digest("toor")) })
	settings.save()
end

login()