Compute HMAC-SHA256 over `data` with `key`. Length is fixed at [`MAC_LEN`].
(key: &MacKey, data: &[u8])
| 88 | /// Compute HMAC-SHA256 over `data` with `key`. Length is fixed at |
| 89 | /// [`MAC_LEN`]. |
| 90 | pub fn compute_hmac(key: &MacKey, data: &[u8]) -> [u8; MAC_LEN] { |
| 91 | let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(key.as_bytes()) |
| 92 | .expect("HMAC-SHA256 accepts any key length"); |
| 93 | mac.update(data); |
| 94 | let out = mac.finalize().into_bytes(); |
| 95 | let mut tag = [0u8; MAC_LEN]; |
| 96 | tag.copy_from_slice(&out); |
| 97 | tag |
| 98 | } |
| 99 | |
| 100 | /// Constant-time verify `tag` against HMAC-SHA256 over `data` with `key`. |
| 101 | /// Returns `Err` on mismatch. |