# Author: Andrieiev Danil Maksimovich # danssg08@gmail.com | https://github.com/DanilAndreev # Here declared bridge functions for storing rainbow table in PostgreSQL database. import psycopg2 CONNECTION = psycopg2.connect( host="localhost", database="postgres", user="postgres", password="postgres", port=5432 ) def get_from_table(end_word: str) -> list: """ get_from_table - function, designed to get records from rainbow table by end_word. :param end_word: End word in chain. :return: Start words of the chain with input end_word. """ cursor = CONNECTION.cursor() try: cursor.execute("select start_word from rainbow_table where end_word = %s", [end_word]) return [x[0] for x in cursor.fetchall()] except Exception as exception: print("Error reading from table:", exception) finally: cursor.close() def add_to_table(start_word: str, end_word: str) -> bool: """ add_to_table :param start_word: Start word of the chain. :param end_word: End word of the chain. :return: True if record was saved and False if not. """ cursor = CONNECTION.cursor() try: cursor.execute("insert into rainbow_table (start_word, end_word) values (%s, %s)", (start_word, end_word)) CONNECTION.commit() return True except Exception as exception: print("Failed to push:", start_word, exception) CONNECTION.rollback() return False finally: cursor.close()