Skip to main content

co_primitives/types/
secret.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 1io BRANDGUARDIAN GmbH
3
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use std::fmt::{Debug, Display};
7use zeroize::Zeroize;
8
9/// Simple wrapper type for secrents to not escape them.
10#[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	/// Create secret from vec.
18	pub fn new(key: Vec<u8>) -> Self {
19		Self(key)
20	}
21
22	/// Divulge (access) the secret.
23	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}