pub const fn validate_hash_bytes(bytes: &[u8]) -> Result<(), VctrlError>Expand description
Validates that a byte slice is exactly HASH_LENGTH bytes long.
§Purpose
This function acts as a gatekeeper to ensure that any byte slice intended
to represent a Hash meets the strict length
invariant (64 bytes) required by the system.
§Design rationale
const fn: Being aconst fnallows this check to be evaluated during compilation if the inputs are known constants. This is useful for verifying hardcoded hashes in configuration or test vectors.- Pre-conditions Check: It is often used as a pre-check before calling
Hash::from_bytesto provide custom error handling or logging before the actual conversion.
§Internal mechanism
The function performs an O(1) comparison between the length of the
provided slice and the constant HASH_LENGTH. If they differ, it returns
a VctrlError::InvalidHashLength containing the incorrect length.
§Errors
Returns VctrlError::InvalidHashLength
if the length of bytes is not exactly equal to HASH_LENGTH (64).
§Examples
Validating a correctly sized slice:
use libvctrl_core::validate::hash::validate_hash_bytes;
use libvctrl_handler::HASH_LENGTH;
let valid_bytes = [0u8; HASH_LENGTH];
assert!(validate_hash_bytes(&valid_bytes).is_ok());Validating an incorrectly sized slice:
use libvctrl_core::validate::hash::validate_hash_bytes;
use libvctrl_handler::{HASH_LENGTH, VctrlError};
let invalid_bytes = [0u8; 32]; // Wrong length
let result = validate_hash_bytes(&invalid_bytes);
assert!(matches!(result, Err(VctrlError::InvalidHashLength(32))));