mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
feat(web): create CipherForm component
This commit is contained in:
@@ -9,6 +9,7 @@ aes.workspace = true
|
||||
cipher-core.workspace = true
|
||||
clap = { workspace = true, optional = true }
|
||||
des.workspace = true
|
||||
strum = { workspace = true, features = ["derive"] }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -74,8 +74,8 @@ impl Algorithm {
|
||||
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",
|
||||
Self::Des => "DES",
|
||||
Self::Aes => "AES",
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,34 @@
|
||||
use crate::{Algorithm, OperationChoice, OutputFormat};
|
||||
use crate::{Algorithm, OperationMode, OutputFormat};
|
||||
use cipher_core::{BlockCipher, CipherResult};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CipherContext {
|
||||
pub algorithm: Algorithm,
|
||||
pub operation: OperationChoice,
|
||||
pub operation: OperationMode,
|
||||
pub key: String,
|
||||
pub input_text: String,
|
||||
pub output_format: OutputFormat,
|
||||
}
|
||||
|
||||
impl CipherContext {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn new(
|
||||
algorithm: Algorithm,
|
||||
operation: OperationMode,
|
||||
key: String,
|
||||
input_text: String,
|
||||
output_format: OutputFormat,
|
||||
) -> Self {
|
||||
Self {
|
||||
algorithm,
|
||||
operation,
|
||||
key,
|
||||
input_text,
|
||||
output_format,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process(&self) -> CipherResult<String> {
|
||||
let text_bytes = self.algorithm.parse_text(&self.input_text)?;
|
||||
let cipher = self.algorithm.new_cipher(&self.key)?;
|
||||
@@ -18,13 +37,13 @@ impl CipherContext {
|
||||
|
||||
fn execute(&self, cipher: &dyn BlockCipher, text_bytes: &[u8]) -> CipherResult<String> {
|
||||
match self.operation {
|
||||
OperationChoice::Encrypt => {
|
||||
OperationMode::Encrypt => {
|
||||
let ciphertext = cipher.encrypt(text_bytes)?;
|
||||
Ok(format!("{ciphertext:X}"))
|
||||
}
|
||||
OperationChoice::Decrypt => {
|
||||
OperationMode::Decrypt => {
|
||||
let plaintext = cipher.decrypt(text_bytes)?;
|
||||
let output = self.output_format.to_string(&plaintext);
|
||||
let output = self.output_format.format(&plaintext);
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,9 @@ mod operation;
|
||||
mod output;
|
||||
|
||||
pub use {
|
||||
algorithm::Algorithm, context::CipherContext, operation::OperationChoice, output::OutputFormat,
|
||||
algorithm::Algorithm, context::CipherContext, operation::OperationMode, output::OutputFormat,
|
||||
};
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{Algorithm, CipherContext, OperationMode, OutputFormat};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,39 @@
|
||||
use std::{convert::Infallible, fmt::Display, str::FromStr};
|
||||
|
||||
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum OperationChoice {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum OperationMode {
|
||||
#[default]
|
||||
Encrypt,
|
||||
Decrypt,
|
||||
}
|
||||
|
||||
impl OperationMode {
|
||||
#[must_use]
|
||||
pub const fn invert(self) -> Self {
|
||||
match self {
|
||||
Self::Encrypt => Self::Decrypt,
|
||||
Self::Decrypt => Self::Encrypt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OperationMode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let s = match self {
|
||||
Self::Encrypt => "Encrypt",
|
||||
Self::Decrypt => "Decrypt",
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for OperationMode {
|
||||
type Err = Infallible;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.trim().to_lowercase().as_ref() {
|
||||
"decrypt" => Ok(Self::Decrypt),
|
||||
_ => Ok(Self::Encrypt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use cipher_core::Output;
|
||||
use std::{convert::Infallible, fmt::Display, str::FromStr};
|
||||
use strum::EnumIter;
|
||||
|
||||
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, EnumIter)]
|
||||
pub enum OutputFormat {
|
||||
/// Binary output
|
||||
Binary,
|
||||
@@ -16,7 +18,7 @@ pub enum OutputFormat {
|
||||
|
||||
impl OutputFormat {
|
||||
#[must_use]
|
||||
pub fn to_string(&self, value: &Output) -> String {
|
||||
pub fn format(&self, value: &Output) -> String {
|
||||
match self {
|
||||
Self::Binary => format!("{value:b}"),
|
||||
Self::Octal => format!("{value:o}"),
|
||||
@@ -25,3 +27,27 @@ impl OutputFormat {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for OutputFormat {
|
||||
type Err = Infallible;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(match s.trim().to_lowercase().as_ref() {
|
||||
"binary" | "bin" => Self::Binary,
|
||||
"octal" | "oct" => Self::Octal,
|
||||
"text" | "txt" => Self::Text,
|
||||
_ => Self::Hex,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OutputFormat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let s = match self {
|
||||
Self::Binary => "Binary",
|
||||
Self::Octal => "Octal",
|
||||
Self::Hex => "Hexadecimal",
|
||||
Self::Text => "Text",
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user