ytls_rustcrypto/hmac/
hmac_sha256.rs

1//! yTLS RustCrypto HMAC SHA256
2
3use hmac::{Hmac, KeyInit, Mac};
4use sha2::{Digest, Sha256};
5use ytls_traits::CryptoSha256HmacProcessor;
6
7/// RustCrypto Sha256Hmac
8#[derive(Clone)]
9pub struct Sha256Hmac {
10    hmac: Hmac<Sha256>,
11}
12
13impl Sha256Hmac {
14    pub fn sha256_hmac_init_with_key(key: &[u8; 32]) -> Self {
15        // SAFETY: Safe to unwrap with fixed legth key
16        let hmac = Hmac::<Sha256>::new_from_slice(key).unwrap();
17
18        Sha256Hmac { hmac }
19    }
20}
21
22impl CryptoSha256HmacProcessor for Sha256Hmac {
23    fn hmac_sha256_update(&mut self, d: &[u8]) -> () {
24        self.hmac.update(d)
25    }
26    fn hmac_sha256_fork(&self) -> Self {
27        self.clone()
28    }
29    fn hmac_sha256_finalize(self) -> [u8; 32] {
30        self.hmac.finalize().into_bytes().into()
31    }
32}