Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import pickle
  3. import uuid
  4. import hashlib
  5. from os import path
  6. import rehash
  7.  
  8. def sign_up():
  9. username = input("Enter the desired username:")
  10. password = input("Enter the desired password:")
  11. salt = uuid.uuid4().hex
  12. hashcode = rehash.sha256(salt.encode() + password.encode())
  13. if path.isfile('database1.pickle'):
  14. with open('database1.pickle', 'rb') as f:
  15. ret=pickle.load(f)
  16. if(username in ret):
  17. return 0
  18. ret[username] = {'SALT' : salt ,
  19. 'HASHCODE' : hashcode
  20. }
  21. else:
  22. with open('database1.pickle','wb') as f:
  23. ret={username : { 'SALT' : salt ,
  24. 'HASHCODE' : hashcode
  25.  
  26. }
  27.  
  28. }
  29. pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
  30. return 1
  31.  
  32. with open('database1.pickle','wb') as f:
  33. pickle.dump(ret, f, protocol=pickle.HIGHEST_PROTOCOL)
  34. return 1
  35.  
  36.  
  37. def sign_in(username):
  38. with open('database1.pickle', 'rb') as f:
  39. b = pickle.load(f)
  40. password = input("Enter your password:")
  41.  
  42. salt = b[username]['SALT']
  43. check = rehash.sha256(salt.encode() + password.encode())
  44. return b[username]['HASHCODE'].hexdigest() == check.hexdigest()
  45.  
  46.  
  47. def change_password(username):
  48. new_password=input("Enter the new password:")
  49. new_salt = uuid.uuid4().hex
  50. new_hashcode = rehash.sha256(new_salt.encode() + new_password.encode())
  51. with open('database1.pickle','rb') as f:
  52. ret = pickle.load(f)
  53.  
  54. ret[username]['SALT'] = new_salt
  55. ret[username]['HASHCODE'] = new_hashcode
  56. with open('database1.pickle','wb') as f:
  57. pickle.dump(ret,f,protocol=pickle.HIGHEST_PROTOCOL)
  58.  
  59.  
  60.  
  61. # In[12]:
  62.  
  63.  
  64. # Main Function
  65. print("*************Hello and Welcome To UNIX Password System***********")
  66. x ='1'
  67. while(x=='1'):
  68. choice = input("Enter 1 for new user signup or 2 for returning users")
  69. if choice == '1' :
  70. ret = sign_up()
  71. if ret==1:
  72. print("User added")
  73. else :
  74. print("Username already taken")
  75. elif choice == '2' :
  76. username = input("Hi User! Please enter your username")
  77. ret = sign_in(username)
  78. if ret > 0 :
  79. username = print("Welcome",username)
  80. choice = input("1 Change Password" + "\n" + "2. Any other key to exit")
  81. if choice == '1':
  82. change_password(username)
  83. print("Password reset successful")
  84. else:
  85. print("See you around")
  86. else :
  87. print("Wrong password")
  88. else:
  89. print("Invalid Selection")
  90. x=input("press 1 for continuing usage or press any key")
  91.  
  92. print("Thank you")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement