use rand::RngCore;
#[derive(Clone)]
pub struct SessionKey {
key: [u8; 32],
}
impl SessionKey {
pub fn generate() -> Self {
let mut key = [0u8; 32];
rand::rng().fill_bytes(&mut key);
Self { key }
}
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() != 32 {
return None;
}
let mut key = [0u8; 32];
key.copy_from_slice(bytes);
Some(Self { key })
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.key
}
pub fn aes_key(&self) -> &[u8] {
&self.key[..16]
}
pub fn hmac_key(&self) -> &[u8] {
&self.key[16..]
}
}
impl std::fmt::Debug for SessionKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SessionKey([REDACTED])")
}
}