Advertisement
Guest User

Password Hashing

a guest
Oct 15th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.35 KB | None | 0 0
  1. import sys, time, os
  2.  
  3. def valid(data, Type):      # Checks if input is valid
  4.     if len(data) == 0:
  5.         return False
  6.     for i in data:
  7.         if Type == "user name":     # for username
  8.             if not (i.isdigit() or i.isalpha() or i == "_"):
  9.                 return False
  10.         if Type == "password":      # for password
  11.             if not(i.isdigit() or i.isalpha() or i in ("! @ # $ % ^ & *").split()):
  12.                 return False
  13.     return True
  14.    
  15. def signUp(Type):       # Sign Up user
  16.     userData = input(": ")
  17.     instruction = {"user name" : "Enter alphabets, numbers or underscores only.", "password" : "Enter alphabets, numbers and the following symbols only: !,@,#,$,%,^,&,*"}
  18.     while not valid(userData, Type):
  19.         print("Invalid %s" % Type)
  20.         print(instruction[Type])
  21.         userData = input(": ")
  22.     return userData
  23.  
  24. def file(mode):         # reads or writes in a file according to the mode
  25.     if mode == "Sign up":
  26.         if os.path.isfile("password.txt"):
  27.             with open("password.txt", "a") as data:
  28.                 data.write(userName1+" "+hashPass(password)+" "+vPassword+"\n")
  29.         else:
  30.             with open("password.txt", "w") as data:
  31.                 data.write(userName1+" "+hashPass(password)+" "+vPassword+"\n")
  32.     if mode == "Log in":
  33.         with open("password.txt", "r") as data:
  34.             userData = []
  35.             rawData = data.readlines()  # reads the file line by line and stores each line as a list
  36.             for line in rawData:
  37.                 userData.append(line.split())
  38.             return userData
  39.  
  40. def encryptUsername(word):  # Encrypts word
  41.     eWord = ""      # encrypted word
  42.     for i in word:
  43.         if ord(i) < 100:    # if the length of ord(i) is 2
  44.             eWord += "0%s" % str(ord(i))
  45.         else:
  46.             eWord += str(ord(i))
  47.     return eWord
  48.  
  49. def hashPass(password):
  50.     ePass = 0 #encrypted password
  51.     for i in password:
  52.         ePass += ord(i)
  53.     ePass = str(ePass)
  54.     while len(ePass) != 1:
  55.         ePass1 = 0   # temporarily store digits of ePass
  56.         for i in ePass:
  57.             ePass1 += int(i)
  58.         ePass = str(ePass1)
  59.     return ePass
  60.  
  61. 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".
  62.     # This function checks if the order of words in a password is correct.
  63.     ePass = ""
  64.     for i in password:
  65.         j = list(str(ord(i)))     # Turns ord(i) into string and makes its list.
  66.         j.reverse()     # Reverses it so that we can get its last digit at first place.
  67.         ePass += j[0]                       # stores the first digit (last digit in the actual)
  68.     return ePass
  69.  
  70. #  - - - - - - - - - - - - - - - - - - Functions end here - - - - - - - - - - - - - - - - - -
  71.  
  72. while True:
  73.     MODE = {"1" : "Log in", "2" : "Sign up", "3" : "Exit"}
  74.     print()
  75.     print("Welcome to Password Hashing Program.")
  76.     if not os.path.isfile("password.txt"):   # if password.txt file not present
  77.         mode = "Sign up"
  78.         print("Please sign up to start.")
  79.     else:
  80.         print("1) Log In \n2) Sign up \n3) Exit")
  81.         mode = ""
  82.         while mode not in ("1 2 3").split():
  83.             mode = input(": ")
  84.         mode = MODE[mode]
  85.     if mode == "Sign up":
  86.         attempts = 0
  87.         checked = False     # To ensure the user confirmed his details
  88.         while not checked:  # While not confirmed
  89.             if attempts == 2:   # Go to start page if not confirmed after 2nd attempt.
  90.                 userName = ""
  91.                 userName1 = ""
  92.                 break
  93.             print()
  94.             print("User name ", end = "")
  95.             userName = signUp("user name")
  96.             userName1 = encryptUsername(userName)
  97.             if os.path.isfile("password.txt"):
  98.                 userInfo = file("Log in")       # gets usernames and passwords from file
  99.                 if userInfo != []:
  100.                     for i in userInfo:
  101.                         if userName1 == i[0]:
  102.                             print("User name '%s' is already used. Please choose another user name." % userName)
  103.                             break
  104.                     if userName1 == i[0]:
  105.                         continue
  106.             print("Password", end = "")
  107.             password = signUp("password")
  108.             print('''Please confirm your account details:
  109. User name: %s
  110. Password : %s''' % (userName, password))
  111.             attempts += 1
  112.             if input("Are the details correct? (Y/N) \n").lower().startswith("y"):
  113.                 checked = True
  114.         vPassword = validatePassOrder(password)
  115.         if checked:
  116.             file(mode)              # Writes encrypted username1 and password to a file
  117.         continue
  118.     if mode == "Log in":
  119.         attempts = 0
  120.         validUser = False
  121.         while not validUser:
  122.             userInfo = file(mode)
  123.             print()
  124.             userName = input("Username: ")
  125.             password = input("Password: ")
  126.             vPassword = validatePassOrder(password)
  127.             for i, j, k in userInfo:
  128.                 if encryptUsername(userName) == i and hashPass(password) == j and vPassword == k:
  129.                     validUser = True
  130.                     break
  131.             if not validUser:
  132.                 attempts += 1
  133.                 print("Access Denied!")
  134.                 print("Invalid user name or password.")
  135.                 if attempts == 3:
  136.                     print("Maximum number of attempts exceeded.")
  137.                     break
  138.     if mode == "Exit":
  139.         print("Thanks for your time.")
  140.         time.sleep(1)
  141.         sys.exit()
  142.     while validUser:     #Game store after log in
  143.         print("Access Granted!")
  144.         time.sleep(1)
  145.         print()
  146.         print("Welcome %s!" % userName)
  147.         print("- - - - - - - -")
  148.         print("You have successfully logged in your account.")
  149.         print("You username and password have been securely stored.")
  150.         while True:
  151.             choose = ""
  152.             while choose not in ("1 2").split():
  153.                 print()
  154.                 print("Choose the options: \n1) See your username and password. \n2) Log out")
  155.                 choose = input(": ")
  156.             if choose == "1":
  157.                 print()
  158.                 print("Username = %s \nPassword = %s" % (userName, password))
  159.                 time.sleep(0.6)
  160.             else:       #Log Out
  161.                 userName = ""
  162.                 break
  163.         if choose == "2":
  164.                 break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement