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:
17
cipher-factory/Cargo.toml
Normal file
17
cipher-factory/Cargo.toml
Normal 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
|
||||
30
cipher-factory/src/algorithm.rs
Normal file
30
cipher-factory/src/algorithm.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
5
cipher-factory/src/lib.rs
Normal file
5
cipher-factory/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod algorithm;
|
||||
mod operation;
|
||||
mod output;
|
||||
|
||||
pub use {algorithm::Algorithm, operation::OperationChoice, output::OutputFormat};
|
||||
6
cipher-factory/src/operation.rs
Normal file
6
cipher-factory/src/operation.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum OperationChoice {
|
||||
Encrypt,
|
||||
Decrypt,
|
||||
}
|
||||
27
cipher-factory/src/output.rs
Normal file
27
cipher-factory/src/output.rs
Normal 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}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user