pub struct KeyHasher { /* private fields */ }Expand description
Derives opaque subject keys using HMAC-SHA-256.
Each derivation is domain-separated by the exact policy and scope identifiers. The same normalized subject therefore yields unrelated keys in different policy scopes.
Applications should keep one stable secret per deployment. Rotating it deliberately starts new counters because all derived subject keys change.
The raw secret is not retained after construction. Instead, the hasher
caches key-equivalent, precomputed HMAC state. Cloning a hasher copies that
state; each copy zeroizes its SHA-256 state and buffered input when dropped.
Treat a live KeyHasher as secret material. Debug never
exposes its state.
Implementations§
Source§impl KeyHasher
impl KeyHasher
Sourcepub const MINIMUM_SECRET_LENGTH: usize = 32
pub const MINIMUM_SECRET_LENGTH: usize = 32
Minimum accepted secret length in bytes.
Sourcepub fn new(secret: impl AsRef<[u8]>) -> Result<Self, KeyHasherError>
pub fn new(secret: impl AsRef<[u8]>) -> Result<Self, KeyHasherError>
Constructs a hasher by precomputing zeroizing keyed HMAC state.
The supplied raw secret is borrowed only for construction and is not retained by the returned hasher.
§Errors
Returns KeyHasherError::SecretTooShort unless the secret contains at
least 32 bytes.
Sourcepub fn hash(
&self,
policy_id: &PolicyId,
scope_id: &ScopeId,
subject: impl AsRef<[u8]>,
) -> SubjectKey
pub fn hash( &self, policy_id: &PolicyId, scope_id: &ScopeId, subject: impl AsRef<[u8]>, ) -> SubjectKey
Hashes a normalized subject within an explicit policy and scope.
Normalization is application-owned: two byte strings are treated as distinct subjects even if an application considers them equivalent.
Sourcepub fn hash_for<P: RateLimitPolicy + ?Sized>(
&self,
policy: &P,
subject: impl AsRef<[u8]>,
) -> SubjectKey
pub fn hash_for<P: RateLimitPolicy + ?Sized>( &self, policy: &P, subject: impl AsRef<[u8]>, ) -> SubjectKey
Hashes a normalized subject in a rate-limit policy’s namespace.