I'm trying to get the hang of Rust lifetimes. While I seem to understand them, I don't seem to know the best way to fix it. Here is a function where I am using the *ring* package to generate a SHA256 HMAC. Here is a simplified version of the function that reproduced the problem:
fn sign<'b>(data: &[u8], key: &[u8]) -> &'b [u8] {
let hmac_key = hmac::SigningKey::new(&digest::SHA256, key);
let signature = hmac::sign(&hmac_key, data);
let data = signature.as_ref();
data
}
This doesn't work because signature
doesn't live long enough. That makes sense; as_ref
has a reference to signature
, and signature doesn't live past the end of the function.
as_ref
is the recommended way in *ring* to get a &[u8]
from its Digest
structure as seen in the documentation.
How do I correct the issue where signature
does not live long enough without copying the entire contents of the byte array?