use aead::rand_core::RngCore;
use blake3::derive_key;
use chacha20poly1305::aead::OsRng;
use std::fmt::Display;
#[derive(Clone, Debug)]
pub struct Secret(co_primitives::Secret);
impl Secret {
pub fn new(secret: Vec<u8>) -> Self {
Self(co_primitives::Secret::new(secret))
}
pub fn generate(size: usize) -> Self {
let mut secret: Vec<u8> = vec![0; size];
OsRng.fill_bytes(secret.as_mut_slice());
Self::new(secret)
}
pub fn derive_serect(&self, context: &str) -> Secret {
Secret::new(derive_key(context, self.divulge()).to_vec())
}
pub fn derive_serect_with_salt(&self, context: &str, salt: &Vec<u8>) -> Secret {
let salted_secret = {
let mut with_salt = self.divulge().to_vec();
with_salt.extend_from_slice(salt.as_slice());
Secret::new(with_salt)
};
salted_secret.derive_serect(context)
}
pub fn divulge(&self) -> &[u8] {
self.0.divulge()
}
}
impl Display for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Secret> for co_primitives::Secret {
fn from(val: Secret) -> Self {
val.0
}
}
impl From<co_primitives::Secret> for Secret {
fn from(value: co_primitives::Secret) -> Self {
Self(value)
}
}