Struct HMAC

Source
pub struct HMAC { /* private fields */ }
Expand description

HMAC-SHA256 implementation.

This struct provides both streaming and one-shot APIs for computing HMAC-SHA256.

§Examples

One-shot HMAC computation:

let mac = hmac_sha256::HMAC::mac(b"message data", b"secret key");

Incremental HMAC computation:

let mut hmac = hmac_sha256::HMAC::new(b"secret key");
hmac.update(b"first part");
hmac.update(b"second part");
let mac = hmac.finalize();

Implementations§

Source§

impl HMAC

Source

pub fn mac(input: impl AsRef<[u8]>, k: impl AsRef<[u8]>) -> [u8; 32]

Computes the HMAC-SHA256 of the provided input using the given key in a single operation.

This is a convenience method for simple HMAC operations.

§Arguments
  • input - The message data to authenticate
  • k - The secret key
§Returns

A 32-byte HMAC-SHA256 message authentication code

§Example
let mac = hmac_sha256::HMAC::mac(b"message data", b"secret key");
Source

pub fn new(k: impl AsRef<[u8]>) -> HMAC

Creates a new HMAC-SHA256 instance with the given key.

§Arguments
  • k - The secret key
§Example
let mut hmac = hmac_sha256::HMAC::new(b"secret key");
Source

pub fn update(&mut self, input: impl AsRef<[u8]>)

Absorbs content into the HMAC state.

This method can be called multiple times to incrementally add data to be authenticated.

§Example
let mut hmac = hmac_sha256::HMAC::new(b"secret key");
hmac.update(b"first chunk");
hmac.update(b"second chunk");
let mac = hmac.finalize();
Source

pub fn finalize(self) -> [u8; 32]

Computes the HMAC-SHA256 of all previously absorbed content.

This method consumes the HMAC instance and returns the computed 32-byte authentication code.

§Example
let mut hmac = hmac_sha256::HMAC::new(b"secret key");
hmac.update(b"message data");
let mac: [u8; 32] = hmac.finalize();
Source

pub fn finalize_verify(self, expected: &[u8; 32]) -> bool

Verifies that the HMAC of absorbed content matches the expected MAC.

This provides constant-time comparison to prevent timing attacks.

§Example
let expected = hmac_sha256::HMAC::mac(b"message", b"key");

let mut hmac = hmac_sha256::HMAC::new(b"key");
hmac.update(b"message");
assert!(hmac.finalize_verify(&expected));

let mut hmac = hmac_sha256::HMAC::new(b"key");
hmac.update(b"modified message");
assert!(!hmac.finalize_verify(&expected));
Source

pub fn verify( input: impl AsRef<[u8]>, k: impl AsRef<[u8]>, expected: &[u8; 32], ) -> bool

Verifies that the HMAC of the provided input using the given key matches the expected MAC.

This is a convenience method that combines HMAC computation and verification in a single call. It provides constant-time comparison to prevent timing attacks.

§Arguments
  • input - The message data to authenticate
  • k - The secret key
  • expected - The expected HMAC value to compare against
§Returns

true if the computed HMAC matches the expected value, false otherwise

§Example
let mac = hmac_sha256::HMAC::mac(b"message", b"key");

// Verify in one shot
assert!(hmac_sha256::HMAC::verify(b"message", b"key", &mac));
assert!(!hmac_sha256::HMAC::verify(b"modified message", b"key", &mac));

Trait Implementations§

Source§

impl Clone for HMAC

Source§

fn clone(&self) -> HMAC

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

Auto Trait Implementations§

§

impl Freeze for HMAC

§

impl RefUnwindSafe for HMAC

§

impl Send for HMAC

§

impl Sync for HMAC

§

impl Unpin for HMAC

§

impl UnwindSafe for HMAC

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.