Advertisement
Guest User

Untitled

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