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 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 _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 _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 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 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 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 _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 test_get_int_from_bin(self): bin_string = '00001010' result = LSBStuff.get_int_from_bin(bin_string) self.assertEqual(result, 10)
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