mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
feat(cli): add AES to cli app
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
use cipher_factory::{Algorithm, OperationChoice, OutputFormat};
|
||||
use clap::Parser;
|
||||
use des::Block64;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Clone, Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
@@ -15,12 +13,12 @@ pub struct Args {
|
||||
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)]
|
||||
pub key: Block64,
|
||||
#[arg(short, long, required = true)]
|
||||
pub key: String,
|
||||
|
||||
/// The text to encrypt/decrypt. Can be a string or a path to a file
|
||||
#[arg(value_name = "TEXT", value_parser = Block64::from_str, required = true)]
|
||||
pub text: Block64,
|
||||
#[arg(value_name = "TEXT", required = true)]
|
||||
pub text: String,
|
||||
|
||||
/// Output format for decrypted data
|
||||
#[arg(short = 'f', long)]
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
mod args;
|
||||
|
||||
use crate::args::Args;
|
||||
use aes::{Aes, Block128};
|
||||
use cipher_core::BlockCipher;
|
||||
use cipher_factory::OperationChoice;
|
||||
use cipher_factory::{Algorithm, OperationChoice, OutputFormat};
|
||||
use clap::Parser;
|
||||
use color_eyre::eyre::{Ok, Result};
|
||||
use des::{Block64, Des};
|
||||
use std::str::FromStr;
|
||||
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let Args {
|
||||
operation,
|
||||
@@ -15,18 +19,40 @@ fn main() -> color_eyre::Result<()> {
|
||||
output_format,
|
||||
} = Args::parse();
|
||||
|
||||
match operation {
|
||||
OperationChoice::Encrypt => {
|
||||
let cipher = algorithm.get_cipher(&key);
|
||||
let ciphertext = cipher.encrypt(&text.to_be_bytes())?;
|
||||
println!("{ciphertext:016X}");
|
||||
match algorithm {
|
||||
Algorithm::Des => {
|
||||
let key = Block64::from_str(&key)?;
|
||||
let text = Block64::from_str(&text)?;
|
||||
let cipher = Des::from_key(&key.to_be_bytes());
|
||||
execute_cipher(operation, &cipher, &text.to_be_bytes(), output_format)?;
|
||||
}
|
||||
OperationChoice::Decrypt => {
|
||||
let cipher = algorithm.get_cipher(&key);
|
||||
let plaintext = cipher.decrypt(&text.to_be_bytes())?;
|
||||
let output = output_format.unwrap_or_default().to_string(&plaintext);
|
||||
println!("{output}");
|
||||
Algorithm::Aes => {
|
||||
let key = Block128::from_str(&key)?;
|
||||
let text = Block128::from_str(&text)?;
|
||||
let cipher = Aes::from_key(&key.to_be_bytes());
|
||||
execute_cipher(operation, &cipher, &text.to_be_bytes(), output_format)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_cipher(
|
||||
operation: OperationChoice,
|
||||
cipher: &impl BlockCipher,
|
||||
text_bytes: &[u8],
|
||||
output_format: Option<OutputFormat>,
|
||||
) -> Result<()> {
|
||||
match operation {
|
||||
OperationChoice::Encrypt => {
|
||||
let ciphertext = cipher.encrypt(text_bytes)?;
|
||||
println!("{ciphertext:X}");
|
||||
}
|
||||
OperationChoice::Decrypt => {
|
||||
let plaintext = cipher.decrypt(text_bytes)?;
|
||||
let output = output_format.unwrap_or_default().to_string(&plaintext);
|
||||
println!("{output}");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user