Money_IMP / src /functions.py
1mpreccable's picture
added crypto
7bdb9e5
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import streamlit as st
import os
URI_OS = os.getenv("MONGODB_LOGIN")
def mongodb_connection(db_name: str, collection_name: str) -> tuple:
client = MongoClient(URI_OS, server_api=ServerApi('1'))
db = client[db_name]
collection = db[collection_name]
try:
client.admin.command('ping')
print("MongoDB connection successful")
return True, collection
except Exception as e:
print(f"Error: {e}")
return False, None
def get_keys_from_collection(collection) -> list:
document = collection.find_one()
return document.keys()
def get_from_collection(collection, name: str) -> list:
categories = collection.distinct(name)
return categories
def delete_from_collection(collection, id_to_delete: str) -> None:
collection.delete_one({'_id': id_to_delete})
return None
ALPHABET = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
ALPHABET_SIZE = len(ALPHABET)
def normalize_text(text):
# Приводим к верхнему регистру и убираем символы вне алфавита
return ''.join([c for c in text.upper() if c in ALPHABET])
def get_shift(char):
return ALPHABET.index(char)
def vigenere_encrypt(plaintext, key):
plaintext = normalize_text(plaintext)
key = normalize_text(key)
ciphertext = ''
for i, char in enumerate(plaintext):
shift = get_shift(key[i % len(key)])
char_index = get_shift(char)
new_index = (char_index + shift) % ALPHABET_SIZE
ciphertext += ALPHABET[new_index]
return ciphertext
def vigenere_decrypt(ciphertext, key):
ciphertext = normalize_text(ciphertext)
key = normalize_text(key)
plaintext = ''
for i, char in enumerate(ciphertext):
shift = get_shift(key[i % len(key)])
char_index = get_shift(char)
new_index = (char_index - shift) % ALPHABET_SIZE
plaintext += ALPHABET[new_index]
return plaintext