reth_ecies/
util.rs

1//! Utility functions for hashing and encoding.
2
3use alloy_primitives::B256;
4use hmac::{Hmac, Mac};
5use sha2::{Digest, Sha256};
6
7/// Hashes the input data with SHA256.
8pub(crate) fn sha256(data: &[u8]) -> B256 {
9    B256::from(Sha256::digest(data).as_ref())
10}
11
12/// Produces a `HMAC_SHA256` digest of the `input_data` and `auth_data` with the given `key`.
13/// This is done by accumulating each slice in `input_data` into the HMAC state, then accumulating
14/// the `auth_data` and returning the resulting digest.
15pub(crate) fn hmac_sha256(key: &[u8], input: &[&[u8]], auth_data: &[u8]) -> B256 {
16    let mut hmac = Hmac::<Sha256>::new_from_slice(key).unwrap();
17    for input in input {
18        hmac.update(input);
19    }
20    hmac.update(auth_data);
21    B256::from_slice(&hmac.finalize().into_bytes())
22}