pub struct DigestCtx { /* private fields */ }Expand description
Stateful hash context (EVP_MD_CTX*).
!Clone — use fork() to duplicate mid-stream state.
All stateful operations require &mut self (exclusive ownership).
Implementations§
Source§impl DigestCtx
impl DigestCtx
Sourcepub fn finish(&mut self, out: &mut [u8]) -> Result<usize, ErrorStack>
pub fn finish(&mut self, out: &mut [u8]) -> Result<usize, ErrorStack>
Finalise the hash and write the result into out.
out must be at least alg.output_len() bytes.
Returns the number of bytes written.
§Errors
Returns Err if EVP_DigestFinal_ex fails.
Sourcepub fn finish_xof(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
pub fn finish_xof(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
Finalise with XOF (extendable-output) mode.
Used by SHAKE-128, SHAKE-256. out.len() determines the output length.
§Errors
Sourcepub fn fork(&self) -> Result<DigestCtx, ErrorStack>
pub fn fork(&self) -> Result<DigestCtx, ErrorStack>
Fork the current mid-stream state into a new context.
Equivalent to EVP_MD_CTX_copy_ex — a deep copy of the context state.
Named fork (not clone) to signal the operation is potentially expensive.
§Errors
Sourcepub fn new_empty() -> Result<Self, ErrorStack>
pub fn new_empty() -> Result<Self, ErrorStack>
Allocate an uninitialised EVP_MD_CTX.
The context is not associated with any algorithm yet. Use this when a
raw context handle is needed before a higher-level init call (e.g.
EVP_DigestSignInit_ex).
§Errors
Returns Err if EVP_MD_CTX_new fails.
Sourcepub fn reinit(
&mut self,
alg: &DigestAlg,
params: Option<&Params<'_>>,
) -> Result<(), ErrorStack>
pub fn reinit( &mut self, alg: &DigestAlg, params: Option<&Params<'_>>, ) -> Result<(), ErrorStack>
Reinitialise this context for reuse with the given algorithm.
Calls EVP_DigestInit_ex2. Pass None for params when no extra
initialisation parameters are required.
§Errors
Returns Err if EVP_DigestInit_ex2 fails.
Sourcepub unsafe fn from_ptr(ptr: *mut EVP_MD_CTX) -> Self
pub unsafe fn from_ptr(ptr: *mut EVP_MD_CTX) -> Self
Construct a DigestCtx from a raw, owned EVP_MD_CTX*.
§Safety
ptr must be a valid, non-null EVP_MD_CTX* that the caller is giving up ownership of.
The context need not be initialised with a digest algorithm yet.
Sourcepub fn as_ptr(&self) -> *mut EVP_MD_CTX
pub fn as_ptr(&self) -> *mut EVP_MD_CTX
Return the raw EVP_MD_CTX* pointer. Valid for the lifetime of self.
Used by Signer/Verifier which call EVP_DigestSign*
on this context directly. Returns a mutable pointer because most
OpenSSL EVP functions require EVP_MD_CTX* even for logically
read-only operations.