feat: add CipherContext struct

This commit is contained in:
Kristofers Solo 2025-11-26 01:38:11 +02:00
parent 2254ecaf7c
commit 5f22690ef7
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
4 changed files with 55 additions and 37 deletions

View File

@ -0,0 +1,32 @@
use crate::{Algorithm, OperationChoice, OutputFormat};
use cipher_core::{BlockCipher, CipherResult};
pub struct CipherContext {
pub algorithm: Algorithm,
pub operation: OperationChoice,
pub key: String,
pub input_text: String,
pub output_format: OutputFormat,
}
impl CipherContext {
pub fn process(&self) -> CipherResult<String> {
let text_bytes = self.algorithm.parse_text(&self.input_text)?;
let cipher = self.algorithm.new_cipher(&self.key)?;
self.execute(cipher.as_ref(), &text_bytes)
}
fn execute(&self, cipher: &dyn BlockCipher, text_bytes: &[u8]) -> CipherResult<String> {
match self.operation {
OperationChoice::Encrypt => {
let ciphertext = cipher.encrypt(text_bytes)?;
Ok(format!("{ciphertext:X}"))
}
OperationChoice::Decrypt => {
let plaintext = cipher.decrypt(text_bytes)?;
let output = self.output_format.to_string(&plaintext);
Ok(output)
}
}
}
}

View File

@ -1,5 +1,8 @@
mod algorithm; mod algorithm;
mod context;
mod operation; mod operation;
mod output; mod output;
pub use {algorithm::Algorithm, operation::OperationChoice, output::OutputFormat}; pub use {
algorithm::Algorithm, context::CipherContext, operation::OperationChoice, output::OutputFormat,
};

View File

@ -1,4 +1,4 @@
use cipher_factory::{Algorithm, OperationChoice, OutputFormat}; use cipher_factory::{Algorithm, CipherContext, OperationChoice, OutputFormat};
use clap::Parser; use clap::Parser;
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
@ -24,3 +24,15 @@ pub struct Args {
#[arg(short = 'f', long)] #[arg(short = 'f', long)]
pub output_format: Option<OutputFormat>, pub output_format: Option<OutputFormat>,
} }
impl From<Args> for CipherContext {
fn from(args: Args) -> Self {
Self {
algorithm: args.algorithm,
operation: args.operation,
key: args.key,
input_text: args.text,
output_format: args.output_format.unwrap_or_default(),
}
}
}

View File

@ -1,46 +1,17 @@
mod args; mod args;
use crate::args::Args; use crate::args::Args;
use cipher_core::BlockCipher; use cipher_factory::CipherContext;
use cipher_factory::{self, OperationChoice, OutputFormat};
use clap::Parser; use clap::Parser;
use color_eyre::eyre::{Ok, Result}; use color_eyre::eyre::Result;
fn main() -> Result<()> { fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
let Args { let args = Args::parse();
operation, let context = CipherContext::from(args);
algorithm,
key,
text,
output_format,
} = Args::parse();
let text_bytes = algorithm.parse_text(&text)?; let output = context.process()?;
let cipher = algorithm.new_cipher(&key)?; println!("{output}");
execute_cipher(operation, cipher.as_ref(), &text_bytes, output_format)?;
Ok(())
}
fn execute_cipher(
operation: OperationChoice,
cipher: &dyn BlockCipher,
text_bytes: &[u8],
output_format: Option<OutputFormat>,
) -> Result<()> {
match operation {
OperationChoice::Encrypt => {
let ciphertext = cipher.encrypt(text_bytes)?;
println!("{ciphertext:X}");
}
OperationChoice::Decrypt => {
let plaintext = cipher.decrypt(text_bytes)?;
let output = output_format.unwrap_or_default().to_string(&plaintext);
println!("{output}");
}
}
Ok(()) Ok(())
} }