use bitvec::prelude::*;
#[derive(Clone, PartialEq)]
pub struct SecretKey {
bytes: Vec<u8>,
}
impl std::fmt::Debug for SecretKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.bytes)
}
}
impl SecretKey {
pub fn get_random_secret_key(size: usize) -> Result<Self, String> {
let mut bytes = vec![0; size];
if let Err(_e) = getrandom::getrandom(&mut bytes) {
return Err(String::from("RNG Error"));
};
Ok(Self::from_bytes(&bytes))
}
pub fn to_bits(&self) -> BitVec<Msb0, u8> {
let bytes = self.bytes.iter().rev().copied().collect();
BitVec::<Msb0, u8>::from_vec(bytes)
}
pub fn to_bytes(&self) -> Vec<u8> {
self.bytes.clone()
}
pub fn from_bytes(bytes: &[u8]) -> Self {
Self {
bytes: bytes.to_vec(),
}
}
}