mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2025-12-20 11:04:38 +00:00
feat: add CipherContext struct
This commit is contained in:
parent
2254ecaf7c
commit
5f22690ef7
32
cipher-factory/src/context.rs
Normal file
32
cipher-factory/src/context.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,8 @@
|
||||
mod algorithm;
|
||||
mod context;
|
||||
mod operation;
|
||||
mod output;
|
||||
|
||||
pub use {algorithm::Algorithm, operation::OperationChoice, output::OutputFormat};
|
||||
pub use {
|
||||
algorithm::Algorithm, context::CipherContext, operation::OperationChoice, output::OutputFormat,
|
||||
};
|
||||
|
||||
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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(())
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user