diff --git a/.github/workflows/Build_and_test.yml b/.github/workflows/build_and_test.yml similarity index 100% rename from .github/workflows/Build_and_test.yml rename to .github/workflows/build_and_test.yml diff --git a/src/lib/file/encoder.rs b/src/lib/file/encoder.rs index ed740a4..c9f17e6 100644 --- a/src/lib/file/encoder.rs +++ b/src/lib/file/encoder.rs @@ -287,7 +287,7 @@ impl FileEncoder { self.read_bytes_underlying_data, ); if let Some(enc_info) = &self.encryption_information { - footer.encrypt_directly(enc_info) + footer.encode_encrypted_header_directly(enc_info) } else { Ok(footer.encode_directly()) } diff --git a/src/lib/footer/file_footer.rs b/src/lib/footer/file_footer.rs index be9b2ae..432835e 100644 --- a/src/lib/footer/file_footer.rs +++ b/src/lib/footer/file_footer.rs @@ -12,6 +12,7 @@ use crate::{ Encryption, ZffError, ZffErrorKind, + encryption::EncryptionAlgorithm, FOOTER_IDENTIFIER_FILE_FOOTER, DEFAULT_LENGTH_HEADER_IDENTIFIER, DEFAULT_LENGTH_VALUE_HEADER_LENGTH, @@ -89,22 +90,42 @@ impl FileFooter { } /// encrypts the file footer by the given encryption information and returns the encrypted file footer. - pub fn encrypt_directly(&self, encryption_information: E) -> Result> + /// # Error + /// The method returns an error, if the encryption fails. + pub fn encode_encrypted_header_directly(&self, encryption_information: E) -> Result> where E: Borrow { let mut vec = Vec::new(); - let mut encrypted_content = Encryption::encrypt_file_footer( - &encryption_information.borrow().encryption_key, - self.encode_content(), - self.file_number, - &encryption_information.borrow().algorithm)?; + let encryption_information = encryption_information.borrow(); + let mut encoded_footer = self.encode_encrypted_footer(&encryption_information.encryption_key, &encryption_information.algorithm)?; let identifier = Self::identifier(); - let encoded_header_length = (DEFAULT_LENGTH_HEADER_IDENTIFIER + DEFAULT_LENGTH_VALUE_HEADER_LENGTH + encrypted_content.len()) as u64; //4 bytes identifier + 8 bytes for length + length itself + let encoded_header_length = 4 + 8 + (encoded_footer.len() as u64); //4 bytes identifier + 8 bytes for length + length itself vec.append(&mut identifier.to_be_bytes().to_vec()); vec.append(&mut encoded_header_length.to_le_bytes().to_vec()); - vec.append(&mut encrypted_content); + vec.append(&mut encoded_footer); + + Ok(vec) + } + + fn encode_encrypted_footer(&self, key: K, algorithm: A) -> Result> + where + K: AsRef<[u8]>, + A: Borrow, + { + let mut vec = Vec::new(); + vec.append(&mut Self::version().encode_directly()); + vec.append(&mut self.file_number.encode_directly()); + + let mut data_to_encrypt = Vec::new(); + data_to_encrypt.append(&mut self.encode_content()); + let encrypted_data = Encryption::encrypt_file_footer( + key, data_to_encrypt, + self.file_number, + algorithm + )?; + vec.append(&mut encrypted_data.encode_directly()); Ok(vec) } @@ -124,7 +145,6 @@ impl FileFooter { let mut cursor = Cursor::new(header_content); Self::check_version(&mut cursor)?; let file_number = u64::decode_directly(&mut cursor)?; - let encrypted_data = Vec::::decode_directly(&mut cursor)?; let algorithm = &encryption_information.borrow().algorithm; let decrypted_data = Encryption::decrypt_file_footer( diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 74666e5..33b3c3e 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -19,7 +19,7 @@ pub mod footer; /// Contains several stuff to handle zff container (e.g. create, extend or read zff container). pub mod io; /// Contains some little helper functions -pub(crate) mod helper; +pub mod helper; mod hashing; mod compression; mod encryption;