co_storage/crypto/
secret.rs1use aead::rand_core::RngCore;
5use blake3::derive_key;
6use chacha20poly1305::aead::OsRng;
7use std::fmt::Display;
8
9#[derive(Clone, Debug)]
12pub struct Secret(co_primitives::Secret);
13impl Secret {
14 pub fn new(secret: Vec<u8>) -> Self {
16 Self(co_primitives::Secret::new(secret))
17 }
18
19 pub fn generate(size: usize) -> Self {
21 let mut secret: Vec<u8> = vec![0; size];
22 OsRng.fill_bytes(secret.as_mut_slice());
23 Self::new(secret)
24 }
25
26 pub fn derive_serect(&self, context: &str) -> Secret {
28 Secret::new(derive_key(context, self.divulge()).to_vec())
29 }
30
31 pub fn derive_serect_with_salt(&self, context: &str, salt: &Vec<u8>) -> Secret {
33 let salted_secret = {
35 let mut with_salt = self.divulge().to_vec();
36 with_salt.extend_from_slice(salt.as_slice());
37 Secret::new(with_salt)
38 };
39
40 salted_secret.derive_serect(context)
42 }
43
44 pub fn divulge(&self) -> &[u8] {
46 self.0.divulge()
47 }
48}
49impl Display for Secret {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 write!(f, "{}", self.0)
52 }
53}
54impl From<Secret> for co_primitives::Secret {
55 fn from(val: Secret) -> Self {
56 val.0
57 }
58}
59impl From<co_primitives::Secret> for Secret {
60 fn from(value: co_primitives::Secret) -> Self {
61 Self(value)
62 }
63}