I have the following function:
fn hmac_sha1<'a>(_message: &'a str, _key: &'a str) -> &'a [u8] {
let key: &[u8] = _key.as_bytes();
let message: &[u8] = _message.as_bytes();
let mut mac: Hmac<Sha1> = Hmac::new(Sha1::new(), key);
mac.input(message);
let mac_result: MacResult = mac.result();
mac_result.code()
}
In essence, I want to encode my message using SHA1, and return a pointer to the resulting byte array. When compiling this code, I get the following error.
--> src/main.rs:81:5
|
81 | mac_result.code()
| ----------^^^^^^^
| |
| returns a value referencing data owned by the current function
| `mac_result` is borrowed here
Now, I understand what is wrong here, I just don't know how to fix it. The mac_result
variable's lifetime expires once the function is finished, and a pointer to its internal result will of course not live any longer.
Is there a way extend the lifetime of the mac_result
variable so that I can return the byte array it contains, without converting it to a Vec<u8>
? That's a workable solution, but I'd rather not convert the array if at all possible.