Reminder/bot/auth/crypto.py
2024-10-24 22:19:30 +03:00

29 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from cryptography.fernet import Fernet
PATH_TO_FILE_CRYPT = "./secret.key"
# Функция для генерации ключа шифрования и записи его в файл
def generate_key_to_file():
key = Fernet.generate_key()
with open(PATH_TO_FILE_CRYPT, 'wb') as key_file:
key_file.write(key)
# Функция для чтения ключа из файла
def load_key_from_file() -> bytes:
with open(PATH_TO_FILE_CRYPT, 'rb') as key_file:
return key_file.read()
# Функция для шифрования пароля
def encrypt_password(password: str) -> str:
fernet = Fernet(load_key_from_file())
encrypted_password = fernet.encrypt(password.encode())
return encrypted_password.decode()
# Функция для расшифрования пароля
def decrypt_password(encrypted_password: str) -> str:
fernet = Fernet(load_key_from_file())
decrypted_password = fernet.decrypt(encrypted_password.encode())
return decrypted_password.decode()