workflow_encryption/
secret.rs1use crate::imports::*;
6
7#[derive(Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
9pub struct Secret(Vec<u8>);
10
11impl Secret {
12 pub fn new(data: Vec<u8>) -> Self {
13 Self(data)
14 }
15
16 pub fn as_str(&self) -> Result<&str> {
17 Ok(std::str::from_utf8(&self.0)?)
18 }
19
20 pub fn as_slice(&self) -> &[u8] {
21 &self.0
22 }
23
24 pub fn as_slice_mut(&mut self) -> &mut [u8] {
25 &mut self.0
26 }
27}
28
29impl AsRef<[u8]> for Secret {
30 fn as_ref(&self) -> &[u8] {
31 &self.0
32 }
33}
34
35impl From<Vec<u8>> for Secret {
36 fn from(vec: Vec<u8>) -> Self {
37 Secret(vec)
38 }
39}
40
41impl From<&[u8]> for Secret {
42 fn from(slice: &[u8]) -> Self {
43 Secret(slice.to_vec())
44 }
45}
46
47impl From<&str> for Secret {
48 fn from(s: &str) -> Self {
49 Secret(s.trim().as_bytes().to_vec())
50 }
51}
52
53impl From<String> for Secret {
54 fn from(mut s: String) -> Self {
55 let secret = Secret(s.trim().as_bytes().to_vec());
56 s.zeroize();
57 secret
58 }
59}
60
61impl Zeroize for Secret {
62 fn zeroize(&mut self) {
63 self.0.zeroize()
64 }
65}
66
67impl Drop for Secret {
68 fn drop(&mut self) {
69 self.zeroize()
70 }
71}
72
73impl std::fmt::Debug for Secret {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 f.debug_struct("Secret")
76 .field("secret", &"********")
77 .finish()
78 }
79}