Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- # In[1]:
- #!/usr/bin/env python
- import pickle
- import uuid
- import hashlib
- from os import path
- import rehash
- # In[2]:
- ##This section of the code deals with all the functionalities provided to the admin
- def admin_initial():
- username = "admin"
- password = "admin"
- salt = uuid.uuid4().hex
- hashcode = rehash.sha256(salt.encode() + password.encode())
- with open('admin.pickle','wb') as f:
- ret={username : { 'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- }
- pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- def admin():
- admin_user = input("Enter the admin username\n")
- admin_pass = input("Enter the admin password\n")
- with open('admin.pickle', 'rb') as f:
- adminb = pickle.load(f)
- salt = adminb[admin_user]['SALT']
- check = rehash.sha256(salt.encode() + admin_pass.encode())
- return adminb[admin_user]['HASHCODE'].hexdigest() == check.hexdigest()
- def admin_pass_change(username):
- with open('admin.pickle','rb') as f:
- pass_chan = pickle.load(f)
- if username in pass_chan:
- new_pass = input("Enter the new password\n")
- new_salt = uuid.uuid4().hex
- new_hashcode = rehash.sha256(new_salt.encode() + new_pass.encode())
- pass_chan[username]['SALT'] = new_salt
- pass_chan[username]['HASHCODE'] = new_hashcode
- with open('admin.pickle','wb') as f:
- pickle.dump(pass_chan,f,protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- else :
- print("Entered username does not match.\n")
- return 0
- def admin_dump():
- with open('user.pickle','rb') as f:
- dump = pickle.load(f)
- print(dump)
- # In[3]:
- ##THis function deals with regular user signup
- def sign_up():
- username = input("Enter the username:\n")
- password = input("Enter the password:\n")
- salt = uuid.uuid4().hex
- hashcode = rehash.sha256(salt.encode() + password.encode())
- if path.isfile('user.pickle'):
- with open('user.pickle', 'rb') as f:
- up=pickle.load(f)
- if(username in up):
- return 0
- up[username] = {'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- else:
- with open('user.pickle','wb') as f:
- up={username : { 'SALT' : salt ,
- 'HASHCODE' : hashcode
- }
- }
- pickle.dump(up,f,protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- with open('user.pickle','wb') as f:
- pickle.dump(up, f, protocol=pickle.HIGHEST_PROTOCOL)
- return 1
- # In[4]:
- ##This function deals with regular user sign in
- def sign_in(username):
- with open('user.pickle', 'rb') as f:
- _in = pickle.load(f)
- if username in _in:
- password = input("Enter your password:")
- salt = _in[username]['SALT']
- check = rehash.sha256(salt.encode() + password.encode())
- return _in[username]['HASHCODE'].hexdigest() == check.hexdigest()
- else:
- print("Username does not exist.")
- return -1
- # In[5]:
- #This function will help the regular user to change the password
- def change_password(username):
- new_pass = input("Enter the new password")
- new_salt = uuid.uuid4().hex
- new_hashcode = rehash.sha256(new_salt.encode() + new_pass.encode())
- with open('user.pickle','rb') as f:
- change = pickle.load(f)
- change[username]['SALT'] = new_salt
- change[username]['HASHCODE'] = new_hashcode
- with open('user.pickle','wb') as f:
- pickle.dump(change,f,protocol=pickle.HIGHEST_PROTOCOL)
- # In[6]:
- #This is the main function logic
- admin_initial()
- choice = '1'
- while(choice < '4'):
- choice = input("1.Admin Login\n2.New User\n3.Existing User\n4.Exit\n")
- if choice == '1':
- ret_admin = admin()
- if ret_admin > 0:
- print("\nWelcome Administrator\n")
- val = input("1.Change Password\n2.Dump Passwords\n3.Exit\n")
- if val == '1':
- username = input("Re-Enter your admin username\n")
- admin_val = admin_pass_change(username)
- if admin_val == 1:
- print("Password reset successful\n")
- else :
- print("Password could not be reset")
- if val == '2':
- admin_dump()
- else:
- print("You cannot bypass this! Hard luck.\n")
- if choice == '2':
- ret = sign_up()
- if ret == 1:
- print("User added successfully\n")
- else:
- print("User could not be added\n")
- if choice == '3' :
- username = input("Hi User! Please enter your username\n")
- ret = sign_in(username)
- if ret > 0 :
- print("Welcome",username)
- choice = input("1 Change Password" + "\n" + "2. Any other key to exit\n")
- if choice == '1':
- change_password(username)
- print("Password reset successful\n")
- else :
- print("Cannot login in\n")
- print("Bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement