Skip to main content

reddb_server/crypto/
hmac.rs

1//! HMAC helpers used by RedDB storage encryption.
2
3use hmac::{Hmac, KeyInit, Mac};
4use sha2::Sha256;
5
6pub struct HmacCtx {
7    key: Vec<u8>,
8}
9
10impl HmacCtx {
11    pub fn new(key: &[u8]) -> Self {
12        Self { key: key.to_vec() }
13    }
14
15    pub fn sha256(&self, message: &[u8]) -> [u8; 32] {
16        hmac_sha256(&self.key, message)
17    }
18}
19
20pub fn hmac_sha256(key: &[u8], message: &[u8]) -> [u8; 32] {
21    type HmacSha256 = Hmac<Sha256>;
22    let mut mac =
23        HmacSha256::new_from_slice(key).unwrap_or_else(|_| panic!("invalid HMAC key size"));
24    mac.update(message);
25    let result = mac.finalize().into_bytes();
26    let mut out = [0u8; 32];
27    out.copy_from_slice(&result);
28    out
29}
30
31pub fn hmac_sha1(_key: &[u8], _message: &[u8]) -> [u8; 20] {
32    [0u8; 20]
33}
34pub fn hmac_md5(_key: &[u8], _message: &[u8]) -> [u8; 16] {
35    [0u8; 16]
36}
37pub fn hmac_sha384(_key: &[u8], _message: &[u8]) -> [u8; 48] {
38    [0u8; 48]
39}