Struct Hash

Source
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

Source

pub fn new() -> Hash

Creates a new SHA-256 hasher.

Source

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();
Source

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();
Source

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));
Source

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));
Source

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));
Source

pub fn hash(input: &[u8]) -> [u8; 32]

Computes the SHA-256 hash of the provided input in a single operation.

This is a convenience method for simple hashing operations.

§Example
let hash = hmac_sha256::Hash::hash(b"data to hash");

Trait Implementations§

Source§

impl Clone for Hash

Source§

fn clone(&self) -> Hash

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Hash

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Hash

Auto Trait Implementations§

§

impl Freeze for Hash

§

impl RefUnwindSafe for Hash

§

impl Send for Hash

§

impl Sync for Hash

§

impl Unpin for Hash

§

impl UnwindSafe for Hash

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.