36
36
37
37
extern crate alloc;
38
38
39
- use alloc:: boxed:: Box ;
39
+ use alloc:: { boxed:: Box , string :: String } ;
40
40
use core:: {
41
41
any,
42
42
fmt:: { self , Debug } ,
@@ -55,31 +55,31 @@ pub use zeroize;
55
55
///
56
56
/// Access to the secret inner value occurs through the [`ExposeSecret`]
57
57
/// or [`ExposeSecretMut`] traits, which provide methods for accessing the inner secret value.
58
- pub struct SecretBox < S : Zeroize > {
58
+ pub struct SecretBox < S : Zeroize + ? Sized > {
59
59
inner_secret : Box < S > ,
60
60
}
61
61
62
- impl < S : Zeroize > Zeroize for SecretBox < S > {
62
+ impl < S : Zeroize + ? Sized > Zeroize for SecretBox < S > {
63
63
fn zeroize ( & mut self ) {
64
64
self . inner_secret . as_mut ( ) . zeroize ( )
65
65
}
66
66
}
67
67
68
- impl < S : Zeroize > Drop for SecretBox < S > {
68
+ impl < S : Zeroize + ? Sized > Drop for SecretBox < S > {
69
69
fn drop ( & mut self ) {
70
70
self . zeroize ( )
71
71
}
72
72
}
73
73
74
- impl < S : Zeroize > ZeroizeOnDrop for SecretBox < S > { }
74
+ impl < S : Zeroize + ? Sized > ZeroizeOnDrop for SecretBox < S > { }
75
75
76
- impl < S : Zeroize > From < Box < S > > for SecretBox < S > {
76
+ impl < S : Zeroize + ? Sized > From < Box < S > > for SecretBox < S > {
77
77
fn from ( source : Box < S > ) -> Self {
78
78
Self :: new ( source)
79
79
}
80
80
}
81
81
82
- impl < S : Zeroize > SecretBox < S > {
82
+ impl < S : Zeroize + ? Sized > SecretBox < S > {
83
83
/// Create a secret value using a pre-boxed value.
84
84
pub fn new ( boxed_secret : Box < S > ) -> Self {
85
85
Self {
@@ -88,24 +88,24 @@ impl<S: Zeroize> SecretBox<S> {
88
88
}
89
89
}
90
90
91
- impl < S : Zeroize + Default > SecretBox < S > {
92
- /// Create a secret value using a function that can initialize the vale in-place.
93
- pub fn new_with_mut ( ctr : impl FnOnce ( & mut S ) ) -> Self {
91
+ impl < S : Zeroize + Default + ? Sized > SecretBox < S > {
92
+ /// Create a secret value using a function that can initialize the value in-place.
93
+ pub fn init_with_mut ( ctr : impl FnOnce ( & mut S ) ) -> Self {
94
94
let mut secret = Self :: default ( ) ;
95
95
ctr ( secret. expose_secret_mut ( ) ) ;
96
96
secret
97
97
}
98
98
}
99
99
100
- impl < S : Zeroize + Clone > SecretBox < S > {
100
+ impl < S : Zeroize + Clone + ? Sized > SecretBox < S > {
101
101
/// Create a secret value using the provided function as a constructor.
102
102
///
103
103
/// The implementation makes an effort to zeroize the locally constructed value
104
104
/// before it is copied to the heap, and constructing it inside the closure minimizes
105
105
/// the possibility of it being accidentally copied by other code.
106
106
///
107
- /// **Note:** using [`Self::new`] or [`Self::new_with_mut `] is preferable when possible,
108
- /// since this method's safety relies on empyric evidence and may be violated on some targets.
107
+ /// **Note:** using [`Self::new`] or [`Self::init_with_mut `] is preferable when possible,
108
+ /// since this method's safety relies on empiric evidence and may be violated on some targets.
109
109
pub fn init_with ( ctr : impl FnOnce ( ) -> S ) -> Self {
110
110
let mut data = ctr ( ) ;
111
111
let secret = Self {
@@ -118,7 +118,7 @@ impl<S: Zeroize + Clone> SecretBox<S> {
118
118
/// Same as [`Self::init_with`], but the constructor can be fallible.
119
119
///
120
120
///
121
- /// **Note:** using [`Self::new`] or [`Self::new_with_mut `] is preferable when possible,
121
+ /// **Note:** using [`Self::new`] or [`Self::init_with_mut `] is preferable when possible,
122
122
/// since this method's safety relies on empyric evidence and may be violated on some targets.
123
123
pub fn try_init_with < E > ( ctr : impl FnOnce ( ) -> Result < S , E > ) -> Result < Self , E > {
124
124
let mut data = ctr ( ) ?;
@@ -130,15 +130,15 @@ impl<S: Zeroize + Clone> SecretBox<S> {
130
130
}
131
131
}
132
132
133
- impl < S : Zeroize + Default > Default for SecretBox < S > {
133
+ impl < S : Zeroize + ? Sized + Default > Default for SecretBox < S > {
134
134
fn default ( ) -> Self {
135
135
Self {
136
136
inner_secret : Box :: < S > :: default ( ) ,
137
137
}
138
138
}
139
139
}
140
140
141
- impl < S : Zeroize > Debug for SecretBox < S > {
141
+ impl < S : Zeroize + ? Sized > Debug for SecretBox < S > {
142
142
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
143
143
write ! ( f, "SecretBox<{}>([REDACTED])" , any:: type_name:: <S >( ) )
144
144
}
@@ -155,29 +155,42 @@ where
155
155
}
156
156
}
157
157
158
- impl < S : Zeroize > ExposeSecret < S > for SecretBox < S > {
158
+ impl < S : Zeroize + ? Sized > ExposeSecret < S > for SecretBox < S > {
159
159
fn expose_secret ( & self ) -> & S {
160
160
self . inner_secret . as_ref ( )
161
161
}
162
162
}
163
163
164
- impl < S : Zeroize > ExposeSecretMut < S > for SecretBox < S > {
164
+ impl < S : Zeroize + ? Sized > ExposeSecretMut < S > for SecretBox < S > {
165
165
fn expose_secret_mut ( & mut self ) -> & mut S {
166
166
self . inner_secret . as_mut ( )
167
167
}
168
168
}
169
169
170
+ /// Secret string type.
171
+ ///
172
+ /// This is a type alias for [`SecretBox<str>`] which supports some helpful trait impls.
173
+ ///
174
+ /// Notably it has a [`From<String>`] impl which is the preferred method for construction.
175
+ pub type SecretString = SecretBox < str > ;
176
+
177
+ impl From < String > for SecretString {
178
+ fn from ( s : String ) -> Self {
179
+ Self :: from ( s. into_boxed_str ( ) )
180
+ }
181
+ }
182
+
170
183
/// Marker trait for secrets which are allowed to be cloned
171
184
pub trait CloneableSecret : Clone + Zeroize { }
172
185
173
186
/// Expose a reference to an inner secret
174
- pub trait ExposeSecret < S > {
187
+ pub trait ExposeSecret < S : ? Sized > {
175
188
/// Expose secret: this is the only method providing access to a secret.
176
189
fn expose_secret ( & self ) -> & S ;
177
190
}
178
191
179
192
/// Expose a mutable reference to an inner secret
180
- pub trait ExposeSecretMut < S > {
193
+ pub trait ExposeSecretMut < S : ? Sized > {
181
194
/// Expose secret: this is the only method providing access to a secret.
182
195
fn expose_secret_mut ( & mut self ) -> & mut S ;
183
196
}
0 commit comments