mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
feat: make factory lib
This commit is contained in:
@@ -7,7 +7,8 @@ edition = "2024"
|
||||
[dependencies]
|
||||
aes.workspace = true
|
||||
cipher-core.workspace = true
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
cipher-factory = { workspace = true, features = ["clap"] }
|
||||
clap.workspace = true
|
||||
color-eyre.workspace = true
|
||||
des.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::output::OutputFormat;
|
||||
use clap::{Parser, ValueEnum};
|
||||
use cipher_factory::{Algorithm, OperationChoice, OutputFormat};
|
||||
use clap::Parser;
|
||||
use des::Block64;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct Args {
|
||||
|
||||
/// Encryption algorithm
|
||||
#[arg(short, long)]
|
||||
pub algorithm: AlgorithmChoice,
|
||||
pub algorithm: Algorithm,
|
||||
|
||||
/// Key used for encryption/decryption. Can be a string or a path to a file
|
||||
#[arg(short, long, value_parser = Block64::from_str, required = true)]
|
||||
@@ -26,15 +26,3 @@ pub struct Args {
|
||||
#[arg(short = 'f', long)]
|
||||
pub output_format: Option<OutputFormat>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
pub enum AlgorithmChoice {
|
||||
Des,
|
||||
Aes,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
pub enum OperationChoice {
|
||||
Encrypt,
|
||||
Decrypt,
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
use crate::args::AlgorithmChoice;
|
||||
use cipher_core::{BlockCipher, InputBlock};
|
||||
use des::Des;
|
||||
|
||||
impl AlgorithmChoice {
|
||||
#[must_use]
|
||||
pub fn get_cipher(&self, key: &impl InputBlock) -> impl BlockCipher {
|
||||
match self {
|
||||
Self::Des => Des::from_key(key.as_bytes()),
|
||||
Self::Aes => todo!("Must implement AES first"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
mod args;
|
||||
mod cipher;
|
||||
mod output;
|
||||
|
||||
use crate::{
|
||||
args::{Args, OperationChoice},
|
||||
output::OutputFormat,
|
||||
};
|
||||
use crate::args::Args;
|
||||
use cipher_core::BlockCipher;
|
||||
use cipher_factory::OperationChoice;
|
||||
use clap::Parser;
|
||||
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
@@ -28,12 +24,8 @@ fn main() -> color_eyre::Result<()> {
|
||||
OperationChoice::Decrypt => {
|
||||
let cipher = algorithm.get_cipher(&key);
|
||||
let plaintext = cipher.decrypt(&text.to_be_bytes())?;
|
||||
match output_format.unwrap_or_default() {
|
||||
OutputFormat::Binary => println!("{plaintext:064b}"),
|
||||
OutputFormat::Octal => println!("{plaintext:022o}"),
|
||||
OutputFormat::Hex => println!("{plaintext:016X}"),
|
||||
OutputFormat::Text => println!("{plaintext}"),
|
||||
}
|
||||
let output = output_format.unwrap_or_default().to_string(&plaintext);
|
||||
println!("{output}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
use clap::ValueEnum;
|
||||
|
||||
#[derive(Debug, Clone, Default, ValueEnum)]
|
||||
pub enum OutputFormat {
|
||||
/// Binary output
|
||||
Binary,
|
||||
/// Octal output
|
||||
Octal,
|
||||
/// Decimal output
|
||||
#[default]
|
||||
Hex,
|
||||
/// Text output (ASCII)
|
||||
Text,
|
||||
}
|
||||
Reference in New Issue
Block a user