feat(core): add CipherOutput type

This commit is contained in:
2025-10-17 21:35:47 +03:00
parent 74dd8eb27d
commit 76a808e1b3
8 changed files with 140 additions and 15 deletions

View File

@@ -21,6 +21,16 @@ macro_rules! secret_block {
/// Mask to restrict the underlying integer to valid bits.
pub const MASK: $int = $mask;
/// Calculate the number of hex digits needed for the bit width
const fn hex_width() -> usize {
($bits as usize).div_ceil(4)
}
/// Calculate the number of octal digits needed for the bit width
const fn octal_width() -> usize {
($bits as usize).div_ceil(3)
}
#[inline]
#[must_use]
pub const fn new(value: $int) -> Self {
@@ -36,6 +46,37 @@ macro_rules! secret_block {
Self(v & Self::MASK)
}
}
impl From<$name> for $int {
fn from(value: $name) -> $int {
value.0
}
}
impl std::fmt::UpperHex for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$X}", self.0, width = Self::hex_width())
}
}
impl std::fmt::LowerHex for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$x}", self.0, width = Self::hex_width())
}
}
impl std::fmt::Octal for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$o}", self.0, width = Self::octal_width())
}
}
impl std::fmt::Binary for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$b}", self.0, width = $bits)
}
}
};
// Helper: generate conversions_as based on type
(@conversions_as u8) => {

View File

@@ -41,7 +41,7 @@ impl BlockCipher for Des {
&self,
block: &[u8],
action: cipher_core::CipherAction,
) -> cipher_core::CipherResult<Vec<u8>> {
) -> cipher_core::CipherResult<cipher_core::CipherOutput> {
let block_arr: [u8; Self::BLOCK_SIZE] = block
.try_into()
.map_err(|_| CipherError::invalid_block_size(Self::BLOCK_SIZE, block.len()))?;

View File

@@ -4,6 +4,7 @@ use zeroize::ZeroizeOnDrop;
/// 64-bit Key for DES
#[derive(ZeroizeOnDrop)]
pub struct Key([u8; 8]);
impl Key {
#[inline]
#[must_use]

View File

@@ -4,4 +4,4 @@ mod des;
mod key;
pub(crate) mod utils;
pub use {des::Des, key::Subkeys};
pub use des::Des;