pub struct Hash { /* private fields */ }
Expand description
SHA-256 hash implementation.
This struct provides both streaming and one-shot APIs for computing SHA-256 hashes.
§Examples
One-shot hashing:
let hash = hmac_sha256::Hash::hash(b"hello world");
Incremental hashing:
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash = hasher.finalize();
Implementations§
Source§impl Hash
impl Hash
Sourcepub fn update(&mut self, input: impl AsRef<[u8]>)
pub fn update(&mut self, input: impl AsRef<[u8]>)
Absorbs content into the hasher state.
This method can be called multiple times to incrementally add data to be hashed.
§Example
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"first chunk");
hasher.update(b"second chunk");
let hash = hasher.finalize();
Sourcepub fn finalize(self) -> [u8; 32]
pub fn finalize(self) -> [u8; 32]
Computes the SHA-256 hash of all previously absorbed content.
This method consumes the hasher and returns the computed 32-byte digest.
§Example
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"data to hash");
let hash: [u8; 32] = hasher.finalize();
Sourcepub fn finalize_verify(self, expected: &[u8; 32]) -> bool
pub fn finalize_verify(self, expected: &[u8; 32]) -> bool
Verifies that the hash of absorbed content matches the expected digest.
This provides constant-time comparison to prevent timing attacks.
§Example
let expected = hmac_sha256::Hash::hash(b"original data");
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"original data");
assert!(hasher.finalize_verify(&expected));
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"modified data");
assert!(!hasher.finalize_verify(&expected));
Sourcepub fn verify(input: impl AsRef<[u8]>, expected: &[u8; 32]) -> bool
pub fn verify(input: impl AsRef<[u8]>, expected: &[u8; 32]) -> bool
Hashes the provided input and verifies it against the expected digest in a single operation.
This is a convenience method that combines hashing and verification in a single call. It provides constant-time comparison to prevent timing attacks.
§Example
let expected = hmac_sha256::Hash::hash(b"original data");
// Verify in one shot
assert!(hmac_sha256::Hash::verify(b"original data", &expected));
assert!(!hmac_sha256::Hash::verify(b"modified data", &expected));
Sourcepub fn verify_with_ref(self, expected: &[u8]) -> bool
pub fn verify_with_ref(self, expected: &[u8]) -> bool
Verifies that the hash of absorbed content matches the expected digest using a reference.
This method accepts a reference to a slice of bytes and verifies against it using
constant-time comparison to prevent timing attacks. Unlike finalize_verify
, this method
does not require the expected value to be exactly 32 bytes, but will return false
if
the length is not 32 bytes.
§Arguments
expected
- The expected hash value to compare against
§Returns
true
if the computed hash matches the expected value, false
otherwise
§Example
let expected = hmac_sha256::Hash::hash(b"original data");
let mut hasher = hmac_sha256::Hash::new();
hasher.update(b"original data");
assert!(hasher.verify_with_ref(&expected));
// Can also verify with a slice
let expected_slice = &expected[..];
let mut hasher2 = hmac_sha256::Hash::new();
hasher2.update(b"original data");
assert!(hasher2.verify_with_ref(expected_slice));
// Or use the one-shot verification
assert!(hmac_sha256::Hash::verify(b"original data", &expected));