dcrypt_algorithms/error/
validate.rs

1//! Validation utilities for cryptographic primitives
2
3use super::{Error, Result};
4
5/// Validate a parameter condition
6#[inline(always)]
7pub fn parameter(condition: bool, name: &'static str, reason: &'static str) -> Result<()> {
8    if !condition {
9        return Err(Error::param(name, reason));
10    }
11    Ok(())
12}
13
14/// Validate a length
15#[inline(always)]
16pub fn length(context: &'static str, actual: usize, expected: usize) -> Result<()> {
17    if actual != expected {
18        return Err(Error::Length {
19            context,
20            expected,
21            actual,
22        });
23    }
24    Ok(())
25}
26
27/// Validate a minimum length
28#[inline(always)]
29pub fn min_length(context: &'static str, actual: usize, min: usize) -> Result<()> {
30    if actual < min {
31        return Err(Error::Length {
32            context,
33            expected: min,
34            actual,
35        });
36    }
37    Ok(())
38}
39
40/// Validate a maximum length
41#[inline(always)]
42pub fn max_length(context: &'static str, actual: usize, max: usize) -> Result<()> {
43    if actual > max {
44        return Err(Error::Length {
45            context,
46            expected: max,
47            actual,
48        });
49    }
50    Ok(())
51}
52
53/// Validate authentication
54#[inline(always)]
55pub fn authentication(is_valid: bool, algorithm: &'static str) -> Result<()> {
56    if !is_valid {
57        return Err(Error::Authentication { algorithm });
58    }
59    Ok(())
60}