Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import pickle
- import uuid
- import hashlib
- from os import path
- import rehash
- def sign_up():
- username = input("Enter the desired username:")
- password = input("Enter the desired password:")
- salt = uuid.uuid4().hex
- hashcode = rehash.sha256(salt.encode() + password.encode())
- if path.isfile('database1.pickle'):
- with open('database1.pickle', 'rb') as f:
- ret=pickle.load(f)
- if(username in ret):
- return 0
- ret[username] = {'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- else:
- with open('database1.pickle','wb') as f:
- ret={username : { 'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- }
- pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- with open('database1.pickle','wb') as f:
- pickle.dump(ret, f, protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- def sign_in(username):
- with open('database1.pickle', 'rb') as f:
- b = pickle.load(f)
- password = input("Enter your password:")
- salt = b[username]['SALT']
- check = rehash.sha256(salt.encode() + password.encode())
- return b[username]['HASHCODE'].hexdigest() == check.hexdigest()
- def change_password(username):
- new_password=input("Enter the new password:")
- new_salt = uuid.uuid4().hex
- new_hashcode = rehash.sha256(new_salt.encode() + new_password.encode())
- with open('database1.pickle','rb') as f:
- ret = pickle.load(f)
- ret[username]['SALT'] = new_salt
- ret[username]['HASHCODE'] = new_hashcode
- with open('database1.pickle','wb') as f:
- pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
- # In[12]:
- # Main Function
- print("*************Hello and Welcome To UNIX Password System***********")
- x ='1'
- while(x=='1'):
- choice = input("Enter 1 for new user signup or 2 for returning users")
- if choice == '1' :
- ret = sign_up()
- if ret==1:
- print("User added")
- else :
- print("Username already taken")
- elif choice == '2' :
- username = input("Hi User! Please enter your username")
- ret = sign_in(username)
- if ret > 0 :
- username = print("Welcome",username)
- choice = input("1 Change Password" + "\n" + "2. Any other key to exit")
- if choice == '1':
- change_password(username)
- print("Password reset successful")
- else:
- print("See you around")
- else :
- print("Wrong password")
- else:
- print("Invalid Selection")
- x=input("press 1 for continuing usage or press any key")
- print("Thank you")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement