cas_lib/message/
hmac.rs

1
2use super::cas_hmac::CASHMAC;
3use hmac::{Hmac, Mac};
4use sha2::Sha256;
5
6type HmacSha256 = Hmac<Sha256>;
7pub struct HMAC;
8
9impl CASHMAC for HMAC {
10    /// Signs a message using HMAC with SHA-256.
11    /// Returns the signature as a vector of bytes.
12    fn sign(key: Vec<u8>, message: Vec<u8>) -> Vec<u8> {
13        let mut mac = HmacSha256::new_from_slice(&key).unwrap();
14        mac.update(&message);
15        let result = mac.finalize().into_bytes().to_vec();
16        result
17    }
18
19    
20
21    /// Verifies a signature using HMAC with SHA-256.
22    /// Returns true if the signature is valid, false otherwise.
23    fn verify(key: Vec<u8>, message: Vec<u8>, signature: Vec<u8>) -> bool {
24        let mut mac = HmacSha256::new_from_slice(&key).unwrap();
25        mac.update(&message);
26        return mac.verify_slice(&signature).is_ok();
27    }
28
29
30}