#[cfg(any(test, feature = "alloc"))]
use alloc::vec::Vec;
use twine_rs_macros::Tlv;
const PSKC_MAX_SIZE: usize = 16;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Tlv)]
#[tlv(tlv_type = 0x04, tlv_length = 16, derive_inner)]
pub struct Pskc([u8; PSKC_MAX_SIZE]);
impl Pskc {
pub fn random() -> Self {
let mut bytes = [0u8; PSKC_MAX_SIZE];
crate::fill_random_bytes(&mut bytes);
Self(bytes)
}
}
impl core::fmt::Display for Pskc {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for byte in &self.0 {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
#[cfg(any(test, feature = "alloc"))]
impl From<Pskc> for Vec<u8> {
fn from(value: Pskc) -> Self {
value.0.to_vec()
}
}
impl From<Pskc> for u128 {
fn from(value: Pskc) -> Self {
u128::from_be_bytes(value.0)
}
}
impl From<u128> for Pskc {
fn from(pskc: u128) -> Self {
Self(pskc.to_be_bytes())
}
}
impl From<[u8; PSKC_MAX_SIZE]> for Pskc {
fn from(value: [u8; PSKC_MAX_SIZE]) -> Self {
Self(value)
}
}