use crate::{
key_control::*,
cryptography::{
*,
},
};
use std::{
fs::File,
io::Write,
};
impl CryptographicMechanism {
pub fn new() -> Self {
Self::AES
}
pub fn aes() -> Self {
Self::AES
}
pub fn aes_gcm_siv() -> Self {
Self::AesGcmSiv
}
pub fn aes_ctr() -> Self {
Self::AesCtr
}
pub fn aes_xts() -> Self {
Self::AesXts
}
pub fn xchacha20() -> Self {
Self::XChaCha20
}
}
impl Default for CryptographicMechanism {
fn default() -> Self { Self::new() }
}
impl Process {
pub fn encryption() -> Self {
Self::Encryption
}
pub fn decryption() -> Self {
Self::Decryption
}
}
impl KeyEncapMechanism {
pub fn new() -> Self {
Self::Kyber1024
}
pub fn kyber1024() -> Self {
Self::Kyber1024
}
pub fn kyber768() -> Self {
Self::Kyber768
}
pub fn kyber512() -> Self {
Self::Kyber512
}
}
impl Default for KeyEncapMechanism {
fn default() -> Self { Self::new() }
}
impl ContentType {
pub fn new() -> Self {
Self::File
}
pub fn file() -> Self {
Self::File
}
pub fn message() -> Self {
Self::Message
}
pub fn raw_data() -> Self {
Self::RawData
}
}
impl Default for ContentType {
fn default() -> Self { Self::new() }
}
impl CryptographicMetadata {
pub fn new() -> Self {
CryptographicMetadata {
process: Process::encryption(),
encryption_type: CryptographicMechanism::new(),
key_type: KeyEncapMechanism::new(),
content_type: ContentType::new(),
}
}
pub fn from(
process: Process,
encryption_type: CryptographicMechanism,
key_type: KeyEncapMechanism,
content_type: ContentType,
) -> Self {
CryptographicMetadata {
process,
encryption_type,
key_type,
content_type,
}
}
pub fn process(&self) -> Result<Process, CryptError> {
Ok(self.process)
}
pub fn encryption_type(self) -> Result<CryptographicMechanism, CryptError> {
Ok(self.encryption_type)
}
pub fn key_type(self) -> Result<KeyEncapMechanism, CryptError> {
Ok(self.key_type)
}
pub fn content_type(self) -> Result<ContentType, CryptError> {
Ok(self.content_type)
}
}
impl Default for CryptographicMetadata {
fn default() -> Self { Self::new() }
}
impl CryptographicInformation {
pub fn new() -> Self {
CryptographicInformation {
content: Vec::new(),
passphrase: Vec::new(),
metadata: CryptographicMetadata::new(),
safe: false,
location: None
}
}
pub fn contains_file(&self) -> Result<bool, CryptError> {
Ok(self.location.is_some())
}
pub fn set_data(&mut self, data: &[u8]) -> Result<(), CryptError> {
let data = data.to_vec();
self.content = data;
Ok(())
}
fn prepare_file_name_for_saving(&self, file_path: &std::path::Path) -> Result<PathBuf, CryptError> {
let mut new_file_path = file_path.to_path_buf();
if let Some(extension) = file_path.extension().and_then(|ext| ext.to_str()) {
if extension == "enc" {
new_file_path.set_extension("");
} else {
let file_name_with_enc = format!("{}.enc", file_path.to_string_lossy());
new_file_path = file_path.with_file_name(file_name_with_enc);
}
} else {
let file_name_with_enc = format!("{}.enc", file_path.to_string_lossy());
new_file_path = file_path.with_file_name(file_name_with_enc);
}
Ok(new_file_path)
}
pub fn safe_file(&mut self) -> Result<(), CryptError> {
let file_path = self.location().map_err(|_| CryptError::PathError)?;
let file_path_with_enc = self.prepare_file_name_for_saving(&file_path)?;
if let Some(parent_dir) = file_path_with_enc.parent() {
if !parent_dir.is_dir() {
std::fs::create_dir_all(parent_dir).map_err(|_| CryptError::WriteError)?;
}
}
let mut buffer = File::create(&file_path_with_enc).map_err(|_| CryptError::WriteError)?;
buffer.write_all(&self.content).map_err(|_| CryptError::WriteError)?;
Ok(())
}
pub fn safe(&self) -> Result<bool, CryptError> {
Ok(self.safe)
}
pub fn location(&self) -> Result<PathBuf, CryptError> {
match &self.location {
Some(path) => Ok(path.location()?),
_ => Err(CryptError::PathError)
}
}
pub fn from(
content: Vec<u8>,
passphrase: Vec<u8>,
metadata: CryptographicMetadata,
safe: bool,
location: Option<FileMetadata>
) -> Self {
CryptographicInformation {
content,
passphrase,
metadata,
safe,
location
}
}
pub fn content(&self) -> Result<&[u8], CryptError> {
Ok(&self.content)
}
pub fn passphrase(&self) -> Result<&[u8], CryptError> {
Ok(&self.passphrase)
}
}