Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. # In[1]:
  5.  
  6.  
  7. #!/usr/bin/env python
  8. import pickle
  9. import uuid
  10. import hashlib
  11. from os import path
  12. import rehash
  13.  
  14.  
  15. # In[2]:
  16.  
  17.  
  18. ##This section of the code deals with all the functionalities provided to the admin
  19.  
  20. def admin_initial():
  21. username = "admin"
  22. password = "admin"
  23. salt = uuid.uuid4().hex
  24. hashcode = rehash.sha256(salt.encode() + password.encode())
  25. with open('admin.pickle','wb') as f:
  26. ret={username : { 'SALT' : salt ,
  27. 'HASHCODE' : hashcode
  28. }
  29. }
  30. pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
  31. return 1
  32.  
  33. def admin():
  34. admin_user = input("Enter the admin username\n")
  35. admin_pass = input("Enter the admin password\n")
  36. with open('admin.pickle', 'rb') as f:
  37. adminb = pickle.load(f)
  38. salt = adminb[admin_user]['SALT']
  39. check = rehash.sha256(salt.encode() + admin_pass.encode())
  40. return adminb[admin_user]['HASHCODE'].hexdigest() == check.hexdigest()
  41.  
  42. def admin_pass_change(username):
  43. with open('admin.pickle','rb') as f:
  44. pass_chan = pickle.load(f)
  45. if username in pass_chan:
  46. new_pass = input("Enter the new password\n")
  47. new_salt = uuid.uuid4().hex
  48. new_hashcode = rehash.sha256(new_salt.encode() + new_pass.encode())
  49. pass_chan[username]['SALT'] = new_salt
  50. pass_chan[username]['HASHCODE'] = new_hashcode
  51. with open('admin.pickle','wb') as f:
  52. pickle.dump(pass_chan,f,protocol=pickle.HIGHEST_PROTOCOL)
  53. return 1
  54. else :
  55. print("Entered username does not match.\n")
  56. return 0
  57.  
  58. def admin_dump():
  59. with open('user.pickle','rb') as f:
  60. dump = pickle.load(f)
  61. print(dump)
  62.  
  63.  
  64. # In[3]:
  65.  
  66.  
  67. ##THis function deals with regular user signup
  68. def sign_up():
  69. username = input("Enter the username:\n")
  70. password = input("Enter the password:\n")
  71.  
  72. salt = uuid.uuid4().hex
  73. hashcode = rehash.sha256(salt.encode() + password.encode())
  74. if path.isfile('user.pickle'):
  75. with open('user.pickle', 'rb') as f:
  76. up=pickle.load(f)
  77. if(username in up):
  78. return 0
  79. up[username] = {'SALT' : salt ,
  80. 'HASHCODE' : hashcode
  81. }
  82. else:
  83. with open('user.pickle','wb') as f:
  84. up={username : { 'SALT' : salt ,
  85. 'HASHCODE' : hashcode
  86.  
  87. }
  88.  
  89. }
  90. pickle.dump(up,f,protocol=pickle.HIGHEST_PROTOCOL)
  91. return 1
  92. with open('user.pickle','wb') as f:
  93. pickle.dump(up, f, protocol=pickle.HIGHEST_PROTOCOL)
  94. return 1
  95.  
  96.  
  97. # In[4]:
  98.  
  99.  
  100. ##This function deals with regular user sign in
  101. def sign_in(username):
  102. with open('user.pickle', 'rb') as f:
  103. _in = pickle.load(f)
  104. if username in _in:
  105. password = input("Enter your password:")
  106. salt = _in[username]['SALT']
  107. check = rehash.sha256(salt.encode() + password.encode())
  108. return _in[username]['HASHCODE'].hexdigest() == check.hexdigest()
  109. else:
  110. print("Username does not exist.")
  111. return -1
  112.  
  113.  
  114. # In[5]:
  115.  
  116.  
  117. #This function will help the regular user to change the password
  118. def change_password(username):
  119. new_pass = input("Enter the new password")
  120. new_salt = uuid.uuid4().hex
  121. new_hashcode = rehash.sha256(new_salt.encode() + new_pass.encode())
  122. with open('user.pickle','rb') as f:
  123. change = pickle.load(f)
  124.  
  125. change[username]['SALT'] = new_salt
  126. change[username]['HASHCODE'] = new_hashcode
  127. with open('user.pickle','wb') as f:
  128. pickle.dump(change,f,protocol=pickle.HIGHEST_PROTOCOL)
  129.  
  130.  
  131. # In[6]:
  132.  
  133.  
  134. #This is the main function logic
  135. admin_initial()
  136. choice = '1'
  137. while(choice < '4'):
  138. choice = input("1.Admin Login\n2.New User\n3.Existing User\n4.Exit\n")
  139. if choice == '1':
  140. ret_admin = admin()
  141. if ret_admin > 0:
  142. print("\nWelcome Administrator\n")
  143. val = input("1.Change Password\n2.Dump Passwords\n3.Exit\n")
  144. if val == '1':
  145. username = input("Re-Enter your admin username\n")
  146. admin_val = admin_pass_change(username)
  147. if admin_val == 1:
  148. print("Password reset successful\n")
  149. else :
  150. print("Password could not be reset")
  151. if val == '2':
  152. admin_dump()
  153.  
  154. else:
  155. print("You cannot bypass this! Hard luck.\n")
  156.  
  157. if choice == '2':
  158. ret = sign_up()
  159. if ret == 1:
  160. print("User added successfully\n")
  161. else:
  162. print("User could not be added\n")
  163.  
  164. if choice == '3' :
  165. username = input("Hi User! Please enter your username\n")
  166. ret = sign_in(username)
  167. if ret > 0 :
  168. print("Welcome",username)
  169. choice = input("1 Change Password" + "\n" + "2. Any other key to exit\n")
  170. if choice == '1':
  171. change_password(username)
  172. print("Password reset successful\n")
  173.  
  174. else :
  175. print("Cannot login in\n")
  176.  
  177.  
  178. print("Bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement