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

Function compute_hmac

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

Compute HMAC-SHA256 over `data` with `key`. Length is fixed at [`MAC_LEN`].

(key: &MacKey, data: &[u8])

Source from the content-addressed store, hash-verified

88/// Compute HMAC-SHA256 over `data` with `key`. Length is fixed at
89/// [`MAC_LEN`].
90pub 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.

Callers 4

hmac_roundtripFunction · 0.70
hmac_rejects_wrong_keyFunction · 0.70
write_envelopeFunction · 0.70

Calls 4

expectMethod · 0.45
as_bytesMethod · 0.45
updateMethod · 0.45
finalizeMethod · 0.45

Tested by 3

hmac_roundtripFunction · 0.56
hmac_rejects_wrong_keyFunction · 0.56