MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / verify_hmac

Function verify_hmac

nodedb-cluster/src/rpc_codec/mac.rs:102–109  ·  view source on GitHub ↗

Constant-time verify `tag` against HMAC-SHA256 over `data` with `key`. Returns `Err` on mismatch.

(key: &MacKey, data: &[u8], tag: &[u8; MAC_LEN])

Source from the content-addressed store, hash-verified

100/// Constant-time verify `tag` against HMAC-SHA256 over `data` with `key`.
101/// Returns `Err` on mismatch.
102pub fn verify_hmac(key: &MacKey, data: &[u8], tag: &[u8; MAC_LEN]) -> Result<()> {
103 let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(key.as_bytes())
104 .expect("HMAC-SHA256 accepts any key length");
105 mac.update(data);
106 mac.verify_slice(tag).map_err(|_| ClusterError::Codec {
107 detail: "frame MAC verification failed".into(),
108 })
109}
110
111#[cfg(test)]
112mod tests {

Callers 3

hmac_roundtripFunction · 0.85
parse_envelopeFunction · 0.85

Calls 3

expectMethod · 0.45
as_bytesMethod · 0.45
updateMethod · 0.45

Tested by 2

hmac_roundtripFunction · 0.68