mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
feat(factory): add cipher/algorithm helper functions
This commit is contained in:
@@ -10,6 +10,10 @@ pub enum CipherError {
|
||||
/// Input data doesn't match the cipher's block size
|
||||
#[error("Invalid block size: expected {expected} bytes, got {actual}")]
|
||||
InvalidBlockSize { expected: usize, actual: usize },
|
||||
|
||||
/// Error parsing block from string
|
||||
#[error("Error parsing block from string: {0}")]
|
||||
BlockParseError(#[from] BlockError),
|
||||
}
|
||||
|
||||
impl CipherError {
|
||||
|
||||
@@ -6,9 +6,7 @@ use crate::{CipherAction, CipherError, CipherResult, Output};
|
||||
/// Implementers define `transform_impl` to handle the core algorithm,
|
||||
/// while `transform` provides validation and convenience wrappers.
|
||||
pub trait BlockCipher {
|
||||
const BLOCK_SIZE: usize;
|
||||
|
||||
fn from_key(key: &[u8]) -> Self;
|
||||
fn block_size(&self) -> usize;
|
||||
|
||||
/// Core cipher transformation (must be implemented by concrete types).
|
||||
///
|
||||
@@ -24,13 +22,11 @@ pub trait BlockCipher {
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `CipherError::InvalidBlockSize` if `block.len() != BLOCK_SIZE`.
|
||||
/// Returns `CipherError::InvalidBlockSize` if `block.len() != self.block_size()`.
|
||||
fn transform(&self, block: &[u8], action: CipherAction) -> CipherResult<Output> {
|
||||
if block.len() != Self::BLOCK_SIZE {
|
||||
return Err(CipherError::invalid_block_size(
|
||||
Self::BLOCK_SIZE,
|
||||
block.len(),
|
||||
));
|
||||
let block_size = self.block_size();
|
||||
if block.len() != block_size {
|
||||
return Err(CipherError::invalid_block_size(block_size, block.len()));
|
||||
}
|
||||
self.transform_impl(block, action)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user