def _create_new_byte_of_data(current_byte, list_of_pairs, i): bin_repr_current_byte = Stuff.get_bin_str_from_bytearray(current_byte) # current_pairs = Stuff.split_in_two_bits(bin_repr_current_byte) current_pairs = list(bin_repr_current_byte) current_pairs[-1] = list_of_pairs[i] new_byte = Stuff.get_int_from_bin(''.join(current_pairs)) return new_byte
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 _read_the_hash(self, length_of_hash, index, offset): hash_str = '' for i in range(length_of_hash): current_byte = self.main_file[index:index + 1] bin_repr_current_byte = \ Stuff.get_bin_str_from_bytearray(current_byte) current_pairs = list(bin_repr_current_byte) hash_str += current_pairs[-1] index += offset rec_hash = Stuff.get_int_from_bin(hash_str) return index, rec_hash
def _read_the_hash(self, length_of_hash, index, offset): hash_str = '' for i in range(length_of_hash): # считываем хеш current_byte = self.main_file[index: index + 1] bin_repr_current_byte = Stuff.get_bin_str_from_bytearray(current_byte) # current_pairs = Stuff.split_in_two_bits(bin_repr_current_byte) это если кодировать по два бита current_pairs = list(bin_repr_current_byte) hash_str += current_pairs[-1] index += offset rec_hash = Stuff.get_int_from_bin(hash_str) return index, rec_hash
def review_diff_bytes(self, diff_bytes: deque): if len(diff_bytes) != 0: while len(diff_bytes) > 127: self.result_data.append(127) for i in range(127): self.result_data.append(diff_bytes.popleft()) new_byte = Stuff.get_bin_from_int(len(diff_bytes)) self.result_data.append(Stuff.get_int_from_bin(new_byte)) for i in range(len(diff_bytes)): self.result_data.append(diff_bytes.popleft()) return diff_bytes
def __init__(self, path_to_the_file, index_of_start_of_data=None, need_decompression=False): self._need_decompression = need_decompression self.main_file = Stuff.read_the_file(path_to_the_file) if index_of_start_of_data is None: self.index_of_start_of_data = \ Stuff.find_index_of_the_start_of_data(self.main_file) else: self.index_of_start_of_data = index_of_start_of_data self.bits_per_sample = self._get_bits_per_sample()
def decode(self): index = 0 while index != len(self.data): if Stuff.get_bin_from_int(self.data[index]).startswith('1'): bound = int(Stuff.get_bin_from_int(self.data[index])[1:], 2) for i in range(bound): self.result.append(self.data[index + 1]) index += 2 else: for i in range(self.data[index]): index += 1 self.result.append(self.data[index]) index += 1 return self.result
def _read_the_content(self, buffer, length, index, offset): string_repr = '' buffer_index = 0 for i in range(length): current_byte = self.main_file[index: index + 1] bin_repr_current_byte = Stuff.get_bin_str_from_bytearray(current_byte) # current_pairs = Stuff.split_in_two_bits(bin_repr_current_byte) current_pairs = list(bin_repr_current_byte) string_repr += current_pairs[-1] if len(string_repr) == 8: buffer[buffer_index] = Stuff.get_int_from_bin(string_repr) string_repr = '' buffer_index += 1 index += offset return index
def _read_the_flag(self, bytes_to_describe, index, offset): description = '' for i in range(bytes_to_describe): current_byte = self.main_file[index: index + 1] bin_repr_current_byte = Stuff.get_bin_str_from_bytearray(current_byte) list_of_current_chars = list(bin_repr_current_byte) description += list_of_current_chars[-1] index += offset return index, description
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 __init__(self, path_to_file_to_insert: list, path_to_main_file: str, need_compression=False, index_of_start_of_data=None): self._need_compression = need_compression self.inserted_files = path_to_file_to_insert self.pickled_files = self._create_pickle() self.main_file = Stuff.read_the_file(path_to_main_file) self.bits_per_sample = self._get_bits_per_sample(self.main_file) if index_of_start_of_data is None: self.index_of_start_of_data = \ Stuff.find_index_of_the_start_of_data(self.main_file) else: self.index_of_start_of_data = index_of_start_of_data if not self._check_the_opportunity_to_enter(): raise Exception("The file you want to write is too large") self.final_filename = re.search(r'\\?([ _0-9а-яА-Я\w]+[^\\]\w+)?$', path_to_main_file).group(1)
def _create_pickle(self): dict_of_files = {} for element in self.inserted_files: data = Stuff.read_the_file(element) dict_of_files[element] = data result = bytearray(pickle.dumps(dict_of_files)) if not self._need_compression: return result else: rle = RLEEncoder(result) return rle.encode()
def review_the_same_bytes(self, count_of_same, prev_byte): if count_of_same != 0: while count_of_same > 127: self.result_data.append(255) self.result_data.append(prev_byte) count_of_same -= 127 new_byte = '1' + bin(count_of_same)[2:].zfill(7) self.result_data.append(Stuff.get_int_from_bin(new_byte)) self.result_data.append(prev_byte) count_of_same = 0 return count_of_same
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 inscribe(self): bytes_to_describe_length_of_name = 16 bits_of_name = Stuff.get_bin_str_from_bytearray(self.inserted_name) # list_of_pairs_of_name = Stuff.split_in_two_bits(bits_of_name) list_of_pairs_of_name = list(bits_of_name) name_description = self._create_description( list_of_pairs_of_name, bytes_to_describe_length_of_name) bytes_to_describe_length_of_data = 32 bits_to_write = Stuff.get_bin_str_from_bytearray( self.inserted_file) # строка которую записываем # list_of_pairs_to_write = Stuff.split_in_two_bits(bits_to_write) 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 bits_of_hash = Stuff.get_bin_from_int(self.inserted_hash) # hash_pairs = Stuff.split_in_two_bits(bits_of_hash) hash_pairs = list(bits_of_hash) hash_description = self._create_description( hash_pairs, bytes_to_describe_length_of_hash) offset = bytes_per_sample = self.bits_per_sample // 8 index = self.index_of_start_of_data + bytes_per_sample 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_name, name_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) index = self.describe_data_and_get_index(list_of_pairs_of_name, index, offset) self.describe_data_and_get_index(list_of_pairs_to_write, index, offset) with open('new_wav.wav', 'wb') as file: file.write(self.main_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_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 _create_new_byte_of_flag(current_byte, description, i): bin_repr_current_byte = Stuff.get_bin_str_from_bytearray(current_byte) list_of_current_chars = list(bin_repr_current_byte) list_of_current_chars[-1] = description[i] new_byte = Stuff.get_int_from_bin(''.join(list_of_current_chars)) return new_byte
def test_bin_str_from_bytearray(self): test_array = bytearray(bytes('а она ему', 'utf-8')) result = LSBStuff.get_bin_str_from_bytearray(test_array) self.assertIsInstance(result, str)
def test_read_the_file(self): with open('filename.txt', 'w') as file: file.write('blablabla') result = LSBStuff.read_the_file('filename.txt') os.remove('filename.txt') self.assertIsInstance(result, bytearray)
def __init__(self, path_to_the_file): self.main_file = Stuff.read_the_file(path_to_the_file) 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()
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)
def test_find_index_of_data(self): test_list = bytearray(bytes('купил мужик шляпу', 'utf-8')) result = LSBStuff.find_index_of_the_start_of_data(test_list) self.assertEqual(result, 7)
def test_get_int_from_bin(self): bin_string = '00001010' result = LSBStuff.get_int_from_bin(bin_string) self.assertEqual(result, 10)
def test_split_into_two_bits(self): test_string = '01001010' result = LSBStuff.split_in_two_bits(test_string) expectation = ['01', '00', '10', '10'] self.assertEqual(result, expectation)
def test_split_string_into_chars(self): test_string = '01001010' result = LSBStuff.split_the_string_into_chars(test_string) expectation = ['0', '1', '0', '0', '1', '0', '1', '0'] self.assertEqual(result, expectation)
def test_get_bin_from_int(self): byte = 5 result = LSBStuff.get_bin_from_int(byte) self.assertEqual(result, '00000101')