pub mod multithread;
pub mod single;
use crate::context::EnkryptitContext;
use crate::encryption::file_encryption::multithread::{
decrypt_multithread_file, encrypt_multithread_file,
};
use crate::encryption::file_encryption::single::{decrypt_file_single, encrypt_file_single};
use crate::errors::EnkryptitError;
use crate::key::EnkryptitKey;
use crate::metadatas::MetaDatas;
use crate::types::KeyType::{self};
use crate::types::{Mode, ParallelismType};
pub fn encrypt_file(
path: &str,
keytype: &KeyType,
context: &mut EnkryptitContext,
) -> Result<String, EnkryptitError> {
let enkryptit_key: EnkryptitKey =
EnkryptitKey::resolve(Mode::Encrypting, keytype, context, path)?;
let compression = context.resolve_compression(path)?;
let parallelism = context.resolve_parallelism(path)?;
match parallelism {
ParallelismType::Single => encrypt_file_single(path, compression, enkryptit_key),
ParallelismType::MultiThread(threads) => {
encrypt_multithread_file(path, compression, enkryptit_key, threads)
}
ParallelismType::Auto => unreachable!(
"`Auto` should never be reached here, and always infered before reaching this function. There is an error in the code. If you are reading this as an user, please open an Issue."
),
}
}
pub fn decrypt_file(
path: &str,
meta_bytes: &[u8],
payload_offset: u64,
context: &mut EnkryptitContext,
) -> Result<String, EnkryptitError> {
let metadatas: MetaDatas = postcard::from_bytes(meta_bytes)?;
let compression_type = metadatas.compression;
let master_nonce = metadatas.nonce;
let enkryptit_key =
EnkryptitKey::resolve(Mode::Decrypting, &metadatas.key_type, context, path)?;
let parallelism = context.resolve_parallelism(path)?;
match parallelism {
ParallelismType::Single => decrypt_file_single(
path,
payload_offset,
enkryptit_key,
master_nonce,
compression_type,
),
ParallelismType::MultiThread(threads) => decrypt_multithread_file(
path,
payload_offset,
enkryptit_key,
master_nonce,
compression_type,
threads,
),
ParallelismType::Auto => unreachable!(
"`Auto` should never be reached here, and always infered before reaching this function. There is an error in the code. If you are reading this as an user, please open an Issue."
),
}
}