DmitrMakeev commited on
Commit
79a5c43
·
verified ·
1 Parent(s): 52ab16f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -934,9 +934,45 @@ def handle_calculation():
934
 
935
  # Чат
936
 
 
937
  messages = []
938
  users = {}
939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
940
 
941
  @app.route('/chat')
942
  def chat_index():
 
934
 
935
  # Чат
936
 
937
+ # Глобальные переменные
938
  messages = []
939
  users = {}
940
 
941
+ # Шифрование (используем пароль для генерации ключа)
942
+ SECRET_PASSWORD = "my_secret_chat_password" # Можете вдвоём придумать свой
943
+
944
+ def get_cipher():
945
+ """Генерируем ключ шифрования на основе пароля"""
946
+ password = SECRET_PASSWORD.encode()
947
+ salt = b"fixed_salt_for_chat" # Можно сделать уникальным для каждой сессии
948
+ kdf = PBKDF2HMAC(
949
+ algorithm=hashes.SHA256(),
950
+ length=32,
951
+ salt=salt,
952
+ iterations=100000,
953
+ )
954
+ key = base64.urlsafe_b64encode(kdf.derive(password))
955
+ return Fernet(key)
956
+
957
+ def encrypt_message(text):
958
+ """Шифруем сообщение"""
959
+ cipher = get_cipher()
960
+ encrypted_text = cipher.encrypt(text.encode())
961
+ return encrypted_text.decode()
962
+
963
+ def decrypt_message(encrypted_text):
964
+ """Расшифровываем сообщение"""
965
+ try:
966
+ cipher = get_cipher()
967
+ decrypted_text = cipher.decrypt(encrypted_text.encode())
968
+ return decrypted_text.decode()
969
+ except Exception:
970
+ return "[не удалось расшифровать]"
971
+
972
+ @app.route('/')
973
+ def index():
974
+ """Главная страница"""
975
+ return "Добро пожаловать! Перейдите на <a href='/chat'>чат</a>"
976
 
977
  @app.route('/chat')
978
  def chat_index():