co_primitives/types/
secret.rs1use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use std::fmt::{Debug, Display};
7use zeroize::Zeroize;
8
9#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema)]
11pub struct Secret(
12 #[serde(with = "serde_bytes")]
13 #[schemars(with = "Vec<u8>")]
14 Vec<u8>,
15);
16impl Secret {
17 pub fn new(key: Vec<u8>) -> Self {
19 Self(key)
20 }
21
22 pub fn divulge(&self) -> &[u8] {
24 &self.0
25 }
26}
27impl Display for Secret {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 f.write_str("*****")
30 }
31}
32impl Debug for Secret {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 f.debug_struct("Secret").field("secret", &"*****").finish()
35 }
36}
37impl Drop for Secret {
38 fn drop(&mut self) {
39 self.0.zeroize()
40 }
41}
42impl From<Vec<u8>> for Secret {
43 fn from(value: Vec<u8>) -> Self {
44 Self(value)
45 }
46}