Hmac

Struct Hmac 

Source
pub struct Hmac<H: Default + HashAlgorithm, const OUTPUT_SIZE: usize> { /* private fields */ }
Expand description

Hmac<H: Default + HashAlgorithm, const OUTPUT_SIZE: usize> is a generic struct that provides the HMAC (Hash-based Message Authentication Code) in Rust.

In the context of cryptographic hashing, HMAC is an algorithm that combines a specified Hash function (H) and a secret cryptographic key to convert input data into a fixed-size sequence of bytes. The HMAC is responsible for maintaining the internal state of the hashing process and providing methods to add more data and retrieve the resultant Message Authentication Code (MAC).

The Hmac struct is implemented using any hash function available in this crate, making it a flexible choice for HMAC operations.

§Examples

The following examples demonstrate using Hmac with both Hash and Hasher, and illustrate the difference between these approaches:

 use rs_sha3_512::Sha3_512State;
 let data = b"hello";
 let key = b"my secret and secure key";

 // Using Hash
 let mut hmac_hash = Hmac::<Sha3_512State, 64>::new(key);
 data.hash(&mut hmac_hash);
 let result_via_hash = hmac_hash.finish();

 // Using Hasher
 let mut hmac_hasher = Hmac::<Sha3_512State, 64>::new(key);
 hmac_hasher.write(data);
 let result_via_hasher = hmac_hasher.finish();

 // Simulating the Hash inners
 let mut hmac_simulate = Hmac::<Sha3_512State, 64>::new(key);
 hmac_simulate.write_usize(data.len());
 hmac_simulate.write(data);
 let simulated_hash_result = hmac_simulate.finish();

 assert_ne!(result_via_hash, result_via_hasher);
 assert_eq!(result_via_hash, simulated_hash_result);

Note that in the context of HMAC, the hash operation applies a different set of operations than the hasher’s write, which results in different outcomes.

Implementations§

Source§

impl<H, const OUTPUT_SIZE: usize> Hmac<H, OUTPUT_SIZE>
where H: BytesLen + Default + HashAlgorithm<Output = ByteArrayWrapper<OUTPUT_SIZE>>, <H as HashAlgorithm>::Output: From<H>, ByteArrayWrapper<OUTPUT_SIZE>: From<H>,

Source

pub fn new(key: &[u8]) -> Self

Creates a new HMAC context with the given key.

This method initializes a new HMAC context using the provided key. If the key is longer than the block size of the hash algorithm, it is hashed, and the hash is used as the key. If the key is shorter than the block size of the hash algorithm, it is padded with zeros.

The key is split into two halves - one for the inner hash and one for the outer hash. The inner hash is padded with 0x36 and the outer hash is padded with 0x5c.

§Arguments
  • key - A byte slice that holds the key.
§Returns

A new HMAC context.

§Example
use rs_hasher_ctx::HasherContext;
use rs_hmac::Hmac;
use rs_sha3_384::Sha3_384State;

let key = b"my secret and secure key";
let mut hmac = Hmac::<Sha3_384State, 48>::new(key);

hmac.write(b"data");
let u64result = hmac.finish();
let bytes_result = HasherContext::finish(&mut hmac);

assert_eq!(u64result, 0xCD06EBA676832C12);
assert_eq!(
    bytes_result,
    [
        0xC1, 0x4C, 0x21, 0xF6, 0x5A, 0xCA, 0x45, 0x19, 0x91, 0xF3, 0xAB, 0x87, 0x76, 0x6E, 0x7C, 0xDF, 0xC4, 0x50,
        0x6A, 0x18, 0xE7, 0x08, 0xA4, 0x64, 0xFA, 0x0E, 0xCD, 0x4C, 0xC0, 0x97, 0x32, 0xFA, 0x5F, 0x8A, 0x8A, 0x33,
        0x26, 0x0F, 0xE2, 0x65, 0x9C, 0xC3, 0xBF, 0x6E, 0xD1, 0x5E, 0x16, 0xEB
    ]
);
Source

pub fn digest(key: &[u8], msg: &[u8]) -> ByteArrayWrapper<OUTPUT_SIZE>

Computes the HMAC of a message with a key.

This method calculates the HMAC of the provided message using the key from the HMAC context.

§Arguments
  • key - A byte slice that holds the key.
  • msg - A byte slice that holds the message.
§Returns

The HMAC of the message as a byte array wrapper.

§Example
use rs_hmac::Hmac;
use rs_sha1::{Sha1Hasher, Sha1State};

let key = b"key";
let  msg = b"The quick brown fox jumps over the lazy dog";
let resulting_sha1state = Hmac::<Sha1State, 20>::digest(key, msg);

assert_eq!(format!("{:02x}", resulting_sha1state), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9");

Trait Implementations§

Source§

impl<H: Clone + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Clone for Hmac<H, OUTPUT_SIZE>

Source§

fn clone(&self) -> Hmac<H, OUTPUT_SIZE>

Returns a duplicate 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<H: Debug + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Debug for Hmac<H, OUTPUT_SIZE>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: Default + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Default for Hmac<H, OUTPUT_SIZE>

Source§

fn default() -> Hmac<H, OUTPUT_SIZE>

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

impl<H: Hash + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Hash for Hmac<H, OUTPUT_SIZE>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<H, const OUTPUT_SIZE: usize> Hasher for Hmac<H, OUTPUT_SIZE>
where H: Default + HashAlgorithm, <H as HashAlgorithm>::Output: From<H>, ByteArrayWrapper<OUTPUT_SIZE>: From<H>,

Source§

fn finish(&self) -> u64

Returns the hash value for the values written so far. Read more
Source§

fn write(&mut self, bytes: &[u8])

Writes some data into this Hasher. Read more
1.3.0 · Source§

fn write_u8(&mut self, i: u8)

Writes a single u8 into this hasher.
1.3.0 · Source§

fn write_u16(&mut self, i: u16)

Writes a single u16 into this hasher.
1.3.0 · Source§

fn write_u32(&mut self, i: u32)

Writes a single u32 into this hasher.
1.3.0 · Source§

fn write_u64(&mut self, i: u64)

Writes a single u64 into this hasher.
1.26.0 · Source§

fn write_u128(&mut self, i: u128)

Writes a single u128 into this hasher.
1.3.0 · Source§

fn write_usize(&mut self, i: usize)

Writes a single usize into this hasher.
1.3.0 · Source§

fn write_i8(&mut self, i: i8)

Writes a single i8 into this hasher.
1.3.0 · Source§

fn write_i16(&mut self, i: i16)

Writes a single i16 into this hasher.
1.3.0 · Source§

fn write_i32(&mut self, i: i32)

Writes a single i32 into this hasher.
1.3.0 · Source§

fn write_i64(&mut self, i: i64)

Writes a single i64 into this hasher.
1.26.0 · Source§

fn write_i128(&mut self, i: i128)

Writes a single i128 into this hasher.
1.3.0 · Source§

fn write_isize(&mut self, i: isize)

Writes a single isize into this hasher.
Source§

fn write_length_prefix(&mut self, len: usize)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a length prefix into this hasher, as part of being prefix-free. Read more
Source§

fn write_str(&mut self, s: &str)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a single str into this hasher. Read more
Source§

impl<H, const OUTPUT_SIZE: usize> HasherContext<OUTPUT_SIZE> for Hmac<H, OUTPUT_SIZE>
where H: Default + HashAlgorithm, <H as HashAlgorithm>::Output: From<H>, ByteArrayWrapper<OUTPUT_SIZE>: From<H>,

Source§

type Output = ByteArrayWrapper<OUTPUT_SIZE>

Source§

fn finish(&mut self) -> Self::Output

Source§

impl<H: PartialEq + Default + HashAlgorithm, const OUTPUT_SIZE: usize> PartialEq for Hmac<H, OUTPUT_SIZE>

Source§

fn eq(&self, other: &Hmac<H, OUTPUT_SIZE>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<H: Eq + Default + HashAlgorithm, const OUTPUT_SIZE: usize> Eq for Hmac<H, OUTPUT_SIZE>

Source§

impl<H: Default + HashAlgorithm, const OUTPUT_SIZE: usize> StructuralPartialEq for Hmac<H, OUTPUT_SIZE>

Auto Trait Implementations§

§

impl<H, const OUTPUT_SIZE: usize> Freeze for Hmac<H, OUTPUT_SIZE>
where <H as HashAlgorithm>::Padding: Freeze, H: Freeze,

§

impl<H, const OUTPUT_SIZE: usize> RefUnwindSafe for Hmac<H, OUTPUT_SIZE>

§

impl<H, const OUTPUT_SIZE: usize> Send for Hmac<H, OUTPUT_SIZE>
where <H as HashAlgorithm>::Padding: Send, H: Send,

§

impl<H, const OUTPUT_SIZE: usize> Sync for Hmac<H, OUTPUT_SIZE>
where <H as HashAlgorithm>::Padding: Sync, H: Sync,

§

impl<H, const OUTPUT_SIZE: usize> Unpin for Hmac<H, OUTPUT_SIZE>
where <H as HashAlgorithm>::Padding: Unpin, H: Unpin,

§

impl<H, const OUTPUT_SIZE: usize> UnwindSafe for Hmac<H, OUTPUT_SIZE>

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.