use hmac::{Hmac, KeyInit, Mac}; use sha2::Sha256; use subtle::ConstantTimeEq; type HmacSha256 = Hmac; pub const MAC_LEN: usize = 32; pub fn mac(key: &[u8; 32], parts: &[&[u8]]) -> Vec { let mut mac = ::new_from_slice(key).expect("HMAC accepts any key length"); for part in parts { mac.update(part); } mac.finalize().into_bytes().to_vec() } pub fn verify(key: &[u8; 32], parts: &[&[u8]], tag: &[u8]) -> bool { tag.len() == MAC_LEN && bool::from(mac(key, parts).as_slice().ct_eq(tag)) } pub fn parse_expiry(text: &str) -> Option { let value: i64 = text.parse().ok()?; (value.to_string() == text).then_some(value) }