Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import hashlib
  2. import os
  3. from getpass import getpass
  4. from pathlib import Path
  5. escapes = ''.join([chr(char) for char in range(7, 14)])
  6. lOrS = input("Login or signup: ").lower()
  7. while (lOrS != "login" and lOrS != "signup"):
  8.     lOrS = input("Error. Login or signup: ").lower()
  9. if(lOrS == "signup"):
  10.     userS = input("Username: ")
  11.     while (Path((hashlib.sha512(userS.encode()).hexdigest())).is_dir()):
  12.         userS = input("Error Username taken. Username: ")
  13.     print("Password will not be displayed")
  14.     pwRS = getpass("Enter password: ")
  15.     pwC = getpass("Retype password: ")
  16.     while (pwRS != pwC):
  17.         pwRS = getpass("Enter password: ")
  18.         pwC = getpass("Retype password: ")
  19.     salt = os.urandom(64)
  20.     pwRS = pwRS.encode() + salt
  21.     pwS = hashlib.sha512(pwRS).hexdigest()
  22.     folder = hashlib.sha512(userS.encode()).hexdigest()
  23.     os.makedirs(folder)
  24.     uH = open(folder + '/hash.txt', 'a+')
  25.     uH.write(pwS)
  26.     uH.close()
  27.     uS = open(folder + '/salt.txt', 'ab+')
  28.     uS.write(salt)
  29.     uS.close()
  30. else:
  31.     def login():
  32.         u = input("Enter Username: ")
  33.         p = getpass("Enter Password: ")
  34.         if(os.path.exists(hashlib.sha512(u.encode()).hexdigest())):
  35.             filepath = hashlib.sha512(u.encode()).hexdigest()
  36.             uHP = open(filepath + '/hash.txt', 'r')
  37.             uSP = open(filepath + '/salt.txt', 'rb')
  38.             uHash = uHP.readline()
  39.             uSalt = uSP.read()
  40.             checkhash = p.encode() + uSalt
  41.             if(hashlib.sha512(checkhash).hexdigest() == uHash):
  42.                 print('logged in')
  43.             else:
  44.                 print("incorrect username or password")
  45.                 login()
  46.         else:
  47.             print("incorrect username or password")
  48.             login()
  49.         uSP.close()
  50.         uHP.close()
  51.     login()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement