Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time, os
- def valid(data, Type): # Checks if input is valid
- if len(data) == 0:
- return False
- for i in data:
- if Type == "user name": # for username
- if not (i.isdigit() or i.isalpha() or i == "_"):
- return False
- if Type == "password": # for password
- if not(i.isdigit() or i.isalpha() or i in ("! @ # $ % ^ & *").split()):
- return False
- return True
- def signUp(Type): # Sign Up user
- userData = input(": ")
- instruction = {"user name" : "Enter alphabets, numbers or underscores only.", "password" : "Enter alphabets, numbers and the following symbols only: !,@,#,$,%,^,&,*"}
- while not valid(userData, Type):
- print("Invalid %s" % Type)
- print(instruction[Type])
- userData = input(": ")
- return userData
- def file(mode): # reads or writes in a file according to the mode
- if mode == "Sign up":
- if os.path.isfile("password.txt"):
- with open("password.txt", "a") as data:
- data.write(userName1+" "+hashPass(password)+" "+vPassword+"\n")
- else:
- with open("password.txt", "w") as data:
- data.write(userName1+" "+hashPass(password)+" "+vPassword+"\n")
- if mode == "Log in":
- with open("password.txt", "r") as data:
- userData = []
- rawData = data.readlines() # reads the file line by line and stores each line as a list
- for line in rawData:
- userData.append(line.split())
- return userData
- def encryptUsername(word): # Encrypts word
- eWord = "" # encrypted word
- for i in word:
- if ord(i) < 100: # if the length of ord(i) is 2
- eWord += "0%s" % str(ord(i))
- else:
- eWord += str(ord(i))
- return eWord
- def hashPass(password):
- ePass = 0 #encrypted password
- for i in password:
- ePass += ord(i)
- ePass = str(ePass)
- while len(ePass) != 1:
- ePass1 = 0 # temporarily store digits of ePass
- for i in ePass:
- ePass1 += int(i)
- ePass = str(ePass1)
- return ePass
- def validatePassOrder(password): # Created this function because the user was able to log in with the password "cba" even when the true password was "abc".
- # This function checks if the order of words in a password is correct.
- ePass = ""
- for i in password:
- j = list(str(ord(i))) # Turns ord(i) into string and makes its list.
- j.reverse() # Reverses it so that we can get its last digit at first place.
- ePass += j[0] # stores the first digit (last digit in the actual)
- return ePass
- # - - - - - - - - - - - - - - - - - - Functions end here - - - - - - - - - - - - - - - - - -
- while True:
- MODE = {"1" : "Log in", "2" : "Sign up", "3" : "Exit"}
- print()
- print("Welcome to Password Hashing Program.")
- if not os.path.isfile("password.txt"): # if password.txt file not present
- mode = "Sign up"
- print("Please sign up to start.")
- else:
- print("1) Log In \n2) Sign up \n3) Exit")
- mode = ""
- while mode not in ("1 2 3").split():
- mode = input(": ")
- mode = MODE[mode]
- if mode == "Sign up":
- attempts = 0
- checked = False # To ensure the user confirmed his details
- while not checked: # While not confirmed
- if attempts == 2: # Go to start page if not confirmed after 2nd attempt.
- userName = ""
- userName1 = ""
- break
- print()
- print("User name ", end = "")
- userName = signUp("user name")
- userName1 = encryptUsername(userName)
- if os.path.isfile("password.txt"):
- userInfo = file("Log in") # gets usernames and passwords from file
- if userInfo != []:
- for i in userInfo:
- if userName1 == i[0]:
- print("User name '%s' is already used. Please choose another user name." % userName)
- break
- if userName1 == i[0]:
- continue
- print("Password", end = "")
- password = signUp("password")
- print('''Please confirm your account details:
- User name: %s
- Password : %s''' % (userName, password))
- attempts += 1
- if input("Are the details correct? (Y/N) \n").lower().startswith("y"):
- checked = True
- vPassword = validatePassOrder(password)
- if checked:
- file(mode) # Writes encrypted username1 and password to a file
- continue
- if mode == "Log in":
- attempts = 0
- validUser = False
- while not validUser:
- userInfo = file(mode)
- print()
- userName = input("Username: ")
- password = input("Password: ")
- vPassword = validatePassOrder(password)
- for i, j, k in userInfo:
- if encryptUsername(userName) == i and hashPass(password) == j and vPassword == k:
- validUser = True
- break
- if not validUser:
- attempts += 1
- print("Access Denied!")
- print("Invalid user name or password.")
- if attempts == 3:
- print("Maximum number of attempts exceeded.")
- break
- if mode == "Exit":
- print("Thanks for your time.")
- time.sleep(1)
- sys.exit()
- while validUser: #Game store after log in
- print("Access Granted!")
- time.sleep(1)
- print()
- print("Welcome %s!" % userName)
- print("- - - - - - - -")
- print("You have successfully logged in your account.")
- print("You username and password have been securely stored.")
- while True:
- choose = ""
- while choose not in ("1 2").split():
- print()
- print("Choose the options: \n1) See your username and password. \n2) Log out")
- choose = input(": ")
- if choose == "1":
- print()
- print("Username = %s \nPassword = %s" % (userName, password))
- time.sleep(0.6)
- else: #Log Out
- userName = ""
- break
- if choose == "2":
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement