common_access_token/
utils.rs

1//! Utility functions for Common Access Token
2
3use crate::error::Error;
4use hmac_sha256::HMAC;
5
6/// Compute HMAC-SHA256 signature
7pub fn compute_hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
8    HMAC::mac(data, key).to_vec()
9}
10
11/// Verify HMAC-SHA256 signature
12pub fn verify_hmac_sha256(key: &[u8], data: &[u8], signature: &[u8]) -> Result<(), Error> {
13    let computed_mac = HMAC::mac(data, key);
14
15    if ct_codecs::verify(&computed_mac, signature) {
16        Ok(())
17    } else {
18        Err(Error::SignatureVerification)
19    }
20}
21
22/// Get current timestamp in seconds since Unix epoch
23pub fn current_timestamp() -> u64 {
24    use std::time::{SystemTime, UNIX_EPOCH};
25    SystemTime::now()
26        .duration_since(UNIX_EPOCH)
27        .expect("Time went backwards")
28        .as_secs()
29}