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 , password):
- salt = uuid.uuid4().hex
- hashcode = rehash.sha256(salt.encode() + password.encode())
- if path.isfile('database.pickle'):
- with open('database.pickle', 'rb') as f:
- ret=pickle.load(f)
- if(ret[username]):
- return 0
- ret[username] = {'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- else:
- with open('database.pickle','wb') as f:
- ret={username : { 'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- }
- pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- with open('database.pickle','wb') as f:
- pickle.dump(ret, f, protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- def sign_in(username , password):
- with open('database.pickle', 'rb') as f:
- b = pickle.load(f)
- salt = b[username]['SALT']
- check = rehash.sha256(salt.encode() + password.encode())
- return b[username]['HASHCODE'].hexdigest() == check.hexdigest()
- def change_password(username,newpassword):
- new_salt = uuid.uuid4().hex
- new_hashcode = rehash.sha256(new_salt.encode() + newpassword.encode())
- with open('database.pickle','rb') as f:
- ret = pickle.load(f)
- ret[username]['SALT'] = new_salt
- ret[username]['HASHCODE'] = new_hashcode
- with open('database.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' :
- username = input("Enter the desired username:")
- password = input("Enter the desired password:")
- ret = sign_up(username,password)
- if ret==1:
- print("User added")
- else :
- print("Username already taken")
- elif choice == '2' :
- username = input("Enter your username:")
- password = input("Enter your password:")
- ret = sign_in(username,password)
- if ret > 0 :
- print("Welcome ",username)
- choice = input("If you want to change password press 1 or else press any key")
- if choice == '1':
- new_password=input("Enter the new password:")
- change_password(username,new_password)
- 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