feat(factory): add cipher/algorithm helper functions

This commit is contained in:
2025-11-24 12:02:22 +02:00
parent 051bba33a8
commit 46a47102b9
8 changed files with 120 additions and 41 deletions

View File

@@ -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 {

View File

@@ -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)
}