feat: add CipherContext struct

This commit is contained in:
2025-11-26 01:38:11 +02:00
parent 2254ecaf7c
commit 5f22690ef7
4 changed files with 55 additions and 37 deletions

View File

@@ -1,4 +1,4 @@
use cipher_factory::{Algorithm, OperationChoice, OutputFormat};
use cipher_factory::{Algorithm, CipherContext, OperationChoice, OutputFormat};
use clap::Parser;
#[derive(Debug, Clone, Parser)]
@@ -24,3 +24,15 @@ pub struct Args {
#[arg(short = 'f', long)]
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;
use crate::args::Args;
use cipher_core::BlockCipher;
use cipher_factory::{self, OperationChoice, OutputFormat};
use cipher_factory::CipherContext;
use clap::Parser;
use color_eyre::eyre::{Ok, Result};
use color_eyre::eyre::Result;
fn main() -> Result<()> {
color_eyre::install()?;
let Args {
operation,
algorithm,
key,
text,
output_format,
} = Args::parse();
let args = Args::parse();
let context = CipherContext::from(args);
let text_bytes = algorithm.parse_text(&text)?;
let cipher = algorithm.new_cipher(&key)?;
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}");
}
}
let output = context.process()?;
println!("{output}");
Ok(())
}