mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2025-12-20 11:04:38 +00:00
feat: make factory lib
This commit is contained in:
parent
bfa93c095a
commit
f42080c90a
@ -1,12 +1,14 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = ["aes", "cipher-core", "cli", "des"]
|
members = ["aes", "cipher-core", "cipher-factory", "cli", "des"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
aes = { path = "aes" }
|
aes = { path = "aes" }
|
||||||
color-eyre = "0.6"
|
color-eyre = "0.6"
|
||||||
cipher-core = { path = "cipher-core" }
|
cipher-core = { path = "cipher-core" }
|
||||||
|
cipher-factory = { path = "cipher-factory" }
|
||||||
claims = "0.8"
|
claims = "0.8"
|
||||||
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
des = { path = "des" }
|
des = { path = "des" }
|
||||||
rand = "0.9"
|
rand = "0.9"
|
||||||
rstest = "0.26"
|
rstest = "0.26"
|
||||||
|
|||||||
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}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,7 +7,8 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
aes.workspace = true
|
aes.workspace = true
|
||||||
cipher-core.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
|
color-eyre.workspace = true
|
||||||
des.workspace = true
|
des.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use crate::output::OutputFormat;
|
use cipher_factory::{Algorithm, OperationChoice, OutputFormat};
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::Parser;
|
||||||
use des::Block64;
|
use des::Block64;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ pub struct Args {
|
|||||||
|
|
||||||
/// Encryption algorithm
|
/// Encryption algorithm
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub algorithm: AlgorithmChoice,
|
pub algorithm: Algorithm,
|
||||||
|
|
||||||
/// Key used for encryption/decryption. Can be a string or a path to a file
|
/// 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)]
|
#[arg(short, long, value_parser = Block64::from_str, required = true)]
|
||||||
@ -26,15 +26,3 @@ pub struct Args {
|
|||||||
#[arg(short = 'f', long)]
|
#[arg(short = 'f', long)]
|
||||||
pub output_format: Option<OutputFormat>,
|
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 args;
|
||||||
mod cipher;
|
|
||||||
mod output;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::args::Args;
|
||||||
args::{Args, OperationChoice},
|
|
||||||
output::OutputFormat,
|
|
||||||
};
|
|
||||||
use cipher_core::BlockCipher;
|
use cipher_core::BlockCipher;
|
||||||
|
use cipher_factory::OperationChoice;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
fn main() -> color_eyre::Result<()> {
|
fn main() -> color_eyre::Result<()> {
|
||||||
@ -28,12 +24,8 @@ fn main() -> color_eyre::Result<()> {
|
|||||||
OperationChoice::Decrypt => {
|
OperationChoice::Decrypt => {
|
||||||
let cipher = algorithm.get_cipher(&key);
|
let cipher = algorithm.get_cipher(&key);
|
||||||
let plaintext = cipher.decrypt(&text.to_be_bytes())?;
|
let plaintext = cipher.decrypt(&text.to_be_bytes())?;
|
||||||
match output_format.unwrap_or_default() {
|
let output = output_format.unwrap_or_default().to_string(&plaintext);
|
||||||
OutputFormat::Binary => println!("{plaintext:064b}"),
|
println!("{output}");
|
||||||
OutputFormat::Octal => println!("{plaintext:022o}"),
|
|
||||||
OutputFormat::Hex => println!("{plaintext:016X}"),
|
|
||||||
OutputFormat::Text => println!("{plaintext}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
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,
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user