def __init__(self, path_to_file_to_insert: str, path_to_main_file: str): self.inserted_file = Stuff.read_the_file(path_to_file_to_insert) self.main_file = Stuff.read_the_file(path_to_main_file) if not self._check_the_opportunity_to_enter(): raise Exception("The file you want to write is too large") self.index_of_start_of_data = \ Stuff.find_index_of_the_start_of_data(self.main_file) self.bits_per_sample = self._get_bits_per_sample(self.main_file) self.inserted_name = self._define_filename(path_to_file_to_insert) self.inserted_hash = Stuff.get_hash(self.inserted_file)
def get_hidden_information(self): offset = bytes_per_sample = self.bits_per_sample // 8 index = self.index_of_start_of_data + bytes_per_sample bytes_to_describe_length = 32 index, length_description = self._read_the_flag( bytes_to_describe_length, index, offset) length = Stuff.get_int_from_bin(length_description) length_of_new_bytearray = length // 8 bytes_to_describe_hash = 8 index, length_of_hash_description = self._read_the_flag( bytes_to_describe_hash, index, offset) length_of_hash = Stuff.get_int_from_bin(length_of_hash_description) index, rec_hash = self._read_the_hash(length_of_hash, index, offset) content_of_recieved = bytearray(length_of_new_bytearray) index += self._read_the_content(content_of_recieved, length, index, offset) actual_hash = Stuff.get_hash(content_of_recieved) self._compare_the_checksum(actual_hash, rec_hash) self.create_files(content_of_recieved)
def inscribe(self): offset = bytes_per_sample = self.bits_per_sample // 8 index = self.index_of_start_of_data + bytes_per_sample bytes_to_describe_length_of_data = 32 bits_to_write = Stuff.get_bin_str_from_bytearray(self.pickled_files) list_of_pairs_to_write = list(bits_to_write) length_description = self._create_description( list_of_pairs_to_write, bytes_to_describe_length_of_data) bytes_to_describe_length_of_hash = 8 current_hash = Stuff.get_hash(self.pickled_files) bits_of_hash = Stuff.get_bin_from_int(current_hash) hash_pairs = list(bits_of_hash) hash_description = self._create_description( hash_pairs, bytes_to_describe_length_of_hash) index = self.describe_flag_and_get_index( bytes_to_describe_length_of_data, length_description, index, offset) index = self.describe_flag_and_get_index( bytes_to_describe_length_of_hash, hash_description, index, offset) index = self.describe_data_and_get_index(hash_pairs, index, offset) self.describe_data_and_get_index(list_of_pairs_to_write, index, offset)
def get_hidden_information(self): offset = bytes_per_sample = self.bits_per_sample // 8 index = self.index_of_start_of_data + bytes_per_sample # индекс первого байта bytes_to_describe_length = 32 index, length_description = self._read_the_flag( bytes_to_describe_length, index, offset ) length = Stuff.get_int_from_bin(length_description) length_of_new_bytearray = length // 8 bytes_to_describe_length_of_name = 16 index, length_of_name_description = self._read_the_flag( bytes_to_describe_length_of_name, index, offset ) length_of_name = Stuff.get_int_from_bin(length_of_name_description) length_of_name_array = length_of_name // 8 bytes_to_describe_hash = 8 index, length_of_hash_description = self._read_the_flag( bytes_to_describe_hash, index, offset ) length_of_hash = Stuff.get_int_from_bin(length_of_hash_description) # длина хеша index, rec_hash = self._read_the_hash(length_of_hash, index, offset) name_bytearray = bytearray(length_of_name_array) index = self._read_the_content(name_bytearray, length_of_name, index, offset) name_of_recieved = name_bytearray.decode() content_of_recieved = bytearray(length_of_new_bytearray) self._read_the_content(content_of_recieved, length, index, offset) actual_hash = Stuff.get_hash(content_of_recieved) self._compare_the_checksum(actual_hash, rec_hash) with open('(recieved) ' + name_of_recieved, 'wb') as file: file.write(content_of_recieved)
def test_get_hash_again(self): test_array = bytearray(b'0') result = LSBStuff.get_hash(test_array) self.assertIsInstance(result, int)
def test_get_hash(self): test_array = bytearray(bytes('как раз', 'utf-8')) result = LSBStuff.get_hash(test_array) self.assertIsInstance(result, int)