sentinel_crypto/hash_trait.rs
1use serde_json::Value;
2
3use crate::error::CryptoError;
4
5/// Core trait for hash functions used in sentinel-crypto.
6/// This trait abstracts hashing operations to allow easy switching between
7/// different hash algorithms while maintaining a consistent interface.
8///
9/// Design choice: Trait-based design enables compile-time algorithm selection
10/// and allows for future extensions (e.g., SHA-256, SHA-3) without changing
11/// the API. The trait is sealed to prevent external implementations that
12/// might not meet security requirements.
13pub trait HashFunction: private::Sealed {
14 /// Computes a cryptographic hash of the given JSON data.
15 /// The data is canonicalized via JSON serialization before hashing to
16 /// ensure deterministic results.
17 ///
18 /// # Arguments
19 /// * `data` - The JSON value to hash
20 ///
21 /// # Returns
22 /// A hex-encoded string representing the hash digest
23 ///
24 /// # Errors
25 /// Returns `CryptoError::Hashing` if JSON serialization fails
26 fn hash_data(data: &Value) -> Result<String, CryptoError>;
27}
28
29// Sealing the trait to prevent external implementations
30pub(crate) mod private {
31 pub trait Sealed {}
32}