Advertisement
Guest User

Untitled

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