feat: make factory lib

This commit is contained in:
2025-11-10 09:07:05 +02:00
parent bfa93c095a
commit f42080c90a
11 changed files with 97 additions and 56 deletions

17
cipher-factory/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "cipher-factory"
version = "0.1.0"
edition = "2024"
[dependencies]
aes.workspace = true
cipher-core.workspace = true
clap = { workspace = true, optional = true }
des.workspace = true
[features]
default = []
clap = ["dep:clap"]
[lints]
workspace = true

View File

@@ -0,0 +1,30 @@
use cipher_core::{BlockCipher, InputBlock};
use des::Des;
use std::fmt::Display;
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Algorithm {
Des,
Aes,
}
impl Algorithm {
#[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"),
}
}
}
impl Display for Algorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Des => "Des",
Self::Aes => "Aes",
};
f.write_str(s)
}
}

View File

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

View File

@@ -0,0 +1,6 @@
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationChoice {
Encrypt,
Decrypt,
}

View File

@@ -0,0 +1,27 @@
use cipher_core::Output;
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
/// Binary output
Binary,
/// Octal output
Octal,
/// Decimal output
#[default]
Hex,
/// Text output (ASCII)
Text,
}
impl OutputFormat {
#[must_use]
pub fn to_string(&self, value: &Output) -> String {
match self {
Self::Binary => format!("{value:064b}"),
Self::Octal => format!("{value:022o}"),
Self::Hex => format!("{value:016X}"),
Self::Text => format!("{value}"),
}
}
}