mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
feat(core): add CipherOutput type
This commit is contained in:
@@ -5,5 +5,5 @@ mod types;
|
||||
pub use {
|
||||
error::{CipherError, CipherResult},
|
||||
traits::BlockCipher,
|
||||
types::CipherAction,
|
||||
types::{CipherAction, CipherOutput},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{CipherAction, CipherError, CipherResult};
|
||||
use crate::{CipherAction, CipherError, CipherOutput, CipherResult};
|
||||
|
||||
/// Generic block cipher trait.
|
||||
///
|
||||
@@ -13,7 +13,7 @@ pub trait BlockCipher: Sized {
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `CipherError` if the transformation fails.
|
||||
fn transform_impl(&self, block: &[u8], action: CipherAction) -> CipherResult<Vec<u8>>;
|
||||
fn transform_impl(&self, block: &[u8], action: CipherAction) -> CipherResult<CipherOutput>;
|
||||
|
||||
/// Transforms a block with validation.
|
||||
///
|
||||
@@ -23,7 +23,7 @@ pub trait BlockCipher: Sized {
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `CipherError::InvalidBlockSize` if `block.len() != BLOCK_SIZE`.
|
||||
fn transform(&self, block: &[u8], action: CipherAction) -> CipherResult<Vec<u8>> {
|
||||
fn transform(&self, block: &[u8], action: CipherAction) -> CipherResult<CipherOutput> {
|
||||
if block.len() != Self::BLOCK_SIZE {
|
||||
return Err(CipherError::invalid_block_size(
|
||||
Self::BLOCK_SIZE,
|
||||
@@ -38,7 +38,7 @@ pub trait BlockCipher: Sized {
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `CipherError::InvalidBlockSize` if the plaintext is not exactly `BLOCK_SIZE` bytes.
|
||||
fn encrypt(&self, plaintext: &[u8]) -> CipherResult<Vec<u8>> {
|
||||
fn encrypt(&self, plaintext: &[u8]) -> CipherResult<CipherOutput> {
|
||||
self.transform(plaintext, CipherAction::Encrypt)
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ pub trait BlockCipher: Sized {
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `CipherError::InvalidBlockSize` if the plaintext is not exactly `BLOCK_SIZE` bytes.
|
||||
fn decrypt(&self, ciphertext: &[u8]) -> CipherResult<Vec<u8>> {
|
||||
fn decrypt(&self, ciphertext: &[u8]) -> CipherResult<CipherOutput> {
|
||||
self.transform(ciphertext, CipherAction::Decrypt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,83 @@
|
||||
use std::{
|
||||
fmt::{Binary, Display, LowerHex, Octal, UpperHex},
|
||||
ops::Deref,
|
||||
str::from_utf8,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CipherAction {
|
||||
Encrypt,
|
||||
Decrypt,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CipherOutput(Vec<u8>);
|
||||
|
||||
impl CipherOutput {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn new(value: &[u8]) -> Self {
|
||||
Self(value.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
impl UpperHex for CipherOutput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for byte in &self.0 {
|
||||
write!(f, "{byte:02X}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl LowerHex for CipherOutput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for byte in &self.0 {
|
||||
write!(f, "{byte:02x}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Octal for CipherOutput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for byte in &self.0 {
|
||||
write!(f, "{byte:03o}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Binary for CipherOutput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for byte in &self.0 {
|
||||
write!(f, "{byte:08b}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for CipherOutput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match from_utf8(&self.0) {
|
||||
Ok(s) => f.write_str(s),
|
||||
Err(_) => write!(f, "{self:X}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CipherOutput {
|
||||
type Target = Vec<u8>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for CipherOutput
|
||||
where
|
||||
T: Into<Vec<u8>>,
|
||||
{
|
||||
fn from(value: T) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user