From 262c7decfe2dafe88cf21c792c523b381064bced Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 6 Nov 2025 11:03:29 +0200 Subject: [PATCH] fix(des): 64 bit block creation --- des/src/block/block64.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/des/src/block/block64.rs b/des/src/block/block64.rs index 23482c6..0744cad 100644 --- a/des/src/block/block64.rs +++ b/des/src/block/block64.rs @@ -74,11 +74,6 @@ fn parse_string_to_u64(s: &str) -> Result { return parse_radix(bin_str, 2); } - // 8-character ASCII string conversion to u64 - if trimmed.len() > 8 { - return Err(BlockError::InvalidByteStringLength(trimmed.len())); - } - ascii_string_to_u64(trimmed) } @@ -92,6 +87,10 @@ fn parse_radix(s: &str, radix: u32) -> Result { } fn ascii_string_to_u64(s: &str) -> Result { + if s.len() > 8 { + return Err(BlockError::InvalidByteStringLength(s.len())); + } + if !s.is_ascii() { return Err(BlockError::conversion_error( "u64", @@ -99,10 +98,9 @@ fn ascii_string_to_u64(s: &str) -> Result { )); } - let mut bytes = [0; 8]; - for (idx, byte) in s.bytes().enumerate() { - bytes[idx] = byte; - } + let mut bytes = [0u8; 8]; + let offset = 8 - s.len(); + bytes[offset..].copy_from_slice(s.as_bytes()); Ok(u64::from_be_bytes(bytes)) }