local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
local sha256 = require("sha256")    

if not fs.exists("/password.txt") then

  while true do

    term.clear()
    term.setCursorPos(1,1)

    print("Please create a new password:")
    local newPass = read("*")
    print("Please confirm your password:")
    local confirm = read("*")

    if newPass == confirm then
      local f = fs.open("/password.txt","w")
      local writepass = string.char(unpack(sha256.digest(newPass)))
      f.write(writepass)
      f.close()
      break
    else
      print("Passwords did not match!")
      os.sleep(1)
    end

  end

  else
    while true do

   term.clear()
   term.setCursorPos(1,1)

   print("Please enter your password:")

   term.write("[")
   term.setCursorPos(11,2)
   term.write("]")

   term.setCursorPos(2,2)

   local ps = fs.open("/password.txt","r")
   local crpassword = ps.readAll()
   local password = read("*")
   local hash = string.char(unpack(sha256.digest(password)))

   if hash == crpassword then
   print("Welcome.")
   ps.close()
   os.pullEvent = oldPullEvent
   break
   else
   print("Incorrect Password.")
   os.sleep(1)
   end
  end
end