Advertisement
WhosYourDaddySec

CLI Chat App Template

Nov 20th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. import hashlib
  2.  
  3. class User:
  4. def __init__(self, username, password_hash, profile):
  5. self.username = username
  6. self.password_hash = password_hash
  7. self.profile = profile
  8. self.friends = []
  9. self.direct_messages = []
  10.  
  11. class Message:
  12. def __init__(self, sender, receiver, content):
  13. self.sender = sender
  14. self.receiver = receiver
  15. self.content = content
  16.  
  17. users = {}
  18. messages = []
  19.  
  20. def generate_password_hash(password):
  21. hash_object = hashlib.sha256(password.encode())
  22. return hash_object.hexdigest()
  23.  
  24. def create_account():
  25. new_user = User("", "", "")
  26. new_user.username = input("Enter your desired username: ")
  27.  
  28. if new_user.username in users:
  29. print("Username already exists. Please choose another username.")
  30. return
  31.  
  32. new_user.password_hash = generate_password_hash(input("Enter your password: "))
  33. new_user.profile = input("Create your profile: ")
  34.  
  35. users[new_user.username] = new_user
  36. print("Account created successfully!")
  37.  
  38. def authenticate_user(username, password):
  39. if username in users:
  40. entered_password_hash = generate_password_hash(password)
  41. if entered_password_hash == users[username].password_hash:
  42. print("Authentication successful!")
  43. else :
  44. print("Authentication failed. Incorrect password.")
  45. else :
  46. print("User not found.")
  47.  
  48. def delete_account(username):
  49. if username in users:
  50. del users[username]
  51. print("Account deleted successfully!")
  52. else :
  53. print("User not found.")
  54.  
  55. def encrypt_message(message, key):
  56. encrypted_message = ""
  57. for char in message:
  58. if char.isalpha():
  59. encrypted_message += chr((ord(char) + key - ord('A')) % 26 + ord('A'))
  60. else :
  61. encrypted_message += char
  62. return encrypted_message
  63.  
  64. def secure_send_message(sender, receiver, content):
  65. new_message = Message(sender, receiver, content)
  66. messages.append(new_message)
  67.  
  68. if receiver in users:
  69. encrypted_content = encrypt_message(content, 3) # Example Caesar cipher with key 3
  70. users[receiver].direct_messages.append(encrypted_content)
  71.  
  72. def rotate_password(username):
  73. if username in users:
  74. new_password = input("Enter your new password: ")
  75. users[username].password_hash = generate_password_hash(new_password)
  76. print("Password rotated successfully!")
  77. else :
  78. print("User not found.")
  79.  
  80. def display_messages(username):
  81. if username in users:
  82. print(f"Displaying messages for {
  83. username
  84. }:")
  85. for message in messages:
  86. if message.receiver == username or message.sender == username:
  87. print(f" {
  88. message.sender
  89. } -> {
  90. message.receiver
  91. }: {
  92. message.content
  93. }")
  94. else :
  95. print("User not found.")
  96.  
  97. def display_direct_messages(username):
  98. if username in users:
  99. print(f"Displaying direct messages for {
  100. username
  101. }:")
  102. for encrypted_message in users[username].direct_messages:
  103. decrypted_message = encrypt_message(encrypted_message, -3) # Decrypt with key -3
  104. print(f" {
  105. username
  106. } received: {
  107. decrypted_message
  108. }")
  109. else :
  110. print("User not found.")
  111.  
  112. def main():
  113. choice = None
  114.  
  115. while choice != 0:
  116. print("\n--- Secure Messaging Tool ---")
  117. print("1. Create Account")
  118. print("2. Authenticate User")
  119. print("3. Delete Account")
  120. print("4. Send Message")
  121. print("5. Rotate Password")
  122. print("6. Display Messages")
  123. print("7. Display Direct Messages")
  124. print("0. Exit")
  125.  
  126. choice = int(input("Enter your choice: "))
  127.  
  128. if choice == 1:
  129. create_account()
  130. elif choice == 2:
  131. username = input("Enter your username: ")
  132. password = input("Enter your password: ")
  133. authenticate_user(username, password)
  134. elif choice == 3:
  135. username = input("Enter your username to delete your account: ")
  136. delete_account(username)
  137. elif choice == 4:
  138. sender = input("Enter sender username: ")
  139. receiver = input("Enter receiver username: ")
  140. content = input("Enter message content: ")
  141. secure_send_message(sender, receiver, content)
  142. elif choice == 5:
  143. username = input("Enter your username: ")
  144. rotate_password(username)
  145. elif choice == 6:
  146. username = input("Enter username to display messages: ")
  147. display_messages(username)
  148. elif choice == 7:
  149. username = input("Enter your username: ")
  150. display_direct_messages(username)
  151. elif choice == 0:
  152. print("Exiting program.")
  153. else :
  154. print("Invalid choice. Try again.")
  155.  
  156. if __name__ == "__main__":
  157. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement