icydb/base/validator/hash.rs
1use crate::{design::prelude::*, traits::Validator};
2
3///
4/// Sha256
5///
6/// Validates canonical SHA-256 hex digests.
7/// Accepted values are exactly 64 ASCII hexadecimal characters.
8///
9
10#[validator]
11pub struct Sha256;
12
13impl Validator<str> for Sha256 {
14 fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
15 // length check
16 if s.len() != 64 {
17 ctx.issue(format!("must be 64 characters, got {}", s.len()));
18 return;
19 }
20
21 // hex characters
22 if !s.chars().all(|c| c.is_ascii_hexdigit()) {
23 ctx.issue("must contain only hexadecimal characters (0-9, a-f)".to_string());
24 }
25 }
26}