1use alloc::vec::Vec;
6use core::{
7 fmt::{self, Debug},
8 ops::{Deref, DerefMut},
9};
10use zeroize::Zeroizing;
11
12#[cfg_attr(
13 all(feature = "ffi", not(test)),
14 safer_ffi_gen::ffi_type(clone, opaque)
15)]
16#[derive(Clone, Eq, PartialEq)]
17pub struct Secret(Zeroizing<Vec<u8>>);
19
20impl Debug for Secret {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 crate::debug::pretty_bytes(&self.0).named("Secret").fmt(f)
23 }
24}
25
26#[cfg_attr(all(feature = "ffi", not(test)), safer_ffi_gen::safer_ffi_gen)]
27impl Secret {
28 pub fn as_bytes(&self) -> &[u8] {
29 &self.0
30 }
31}
32
33impl From<Vec<u8>> for Secret {
34 fn from(bytes: Vec<u8>) -> Self {
35 Zeroizing::new(bytes).into()
36 }
37}
38
39impl From<Zeroizing<Vec<u8>>> for Secret {
40 fn from(bytes: Zeroizing<Vec<u8>>) -> Self {
41 Self(bytes)
42 }
43}
44
45impl Deref for Secret {
46 type Target = [u8];
47
48 fn deref(&self) -> &[u8] {
49 &self.0
50 }
51}
52
53impl DerefMut for Secret {
54 fn deref_mut(&mut self) -> &mut [u8] {
55 &mut self.0
56 }
57}