pub struct SecretValue(/* private fields */);Expand description
A secret held as bytes and zeroized when it is dropped.
Debug and Display print ***, so the value does not end up in logs or
panic messages. Clone makes a second buffer, and each copy is zeroized on
its own drop. == takes time that depends on the lengths only, not on the
bytes.
The value is read in two ways:
- borrowing it with
as_sensitive_*andref_sensitive_value, which make no copy; - lending a copy to a closure with
exposed_in_*, which makes sure the copy is zeroized once the closure is done with it.
use secret_vault_value::SecretValue;
let secret_value = SecretValue::from("my-password");
assert_eq!(format!("{secret_value:?}"), "***");
assert_eq!(secret_value.to_string(), "***");
assert_eq!(secret_value.as_sensitive_str(), "my-password");Implementations§
Source§impl SecretValue
impl SecretValue
Sourcepub fn new(src: Vec<u8>) -> SecretValue
pub fn new(src: Vec<u8>) -> SecretValue
Takes ownership of src without copying it.
Sourcepub fn ref_sensitive_value(&self) -> &Vec<u8> ⓘ
pub fn ref_sensitive_value(&self) -> &Vec<u8> ⓘ
Borrows the secret bytes.
Sourcepub fn ref_sensitive_value_mut(&mut self) -> &mut Vec<u8> ⓘ
pub fn ref_sensitive_value_mut(&mut self) -> &mut Vec<u8> ⓘ
Borrows the secret bytes mutably.
Be aware that growing the vector may reallocate it, and the old allocation is freed without being zeroized. Reserve the capacity up front if the value is built in place.
Sourcepub fn sensitive_value_to_str(&self) -> Result<&str, Utf8Error>
pub fn sensitive_value_to_str(&self) -> Result<&str, Utf8Error>
Sourcepub fn secure_clear(&mut self)
pub fn secure_clear(&mut self)
Zeroizes the bytes and leaves the value empty.
use secret_vault_value::SecretValue;
let mut secret_value = SecretValue::from("my-password");
secret_value.secure_clear();
assert!(secret_value.as_sensitive_bytes().is_empty());Sourcepub fn as_sensitive_str(&self) -> &str
pub fn as_sensitive_str(&self) -> &str
Borrows the secret as a string slice.
§Panics
Panics if the bytes are not valid UTF-8. Use
sensitive_value_to_str for binary
secrets.
Sourcepub fn as_sensitive_bytes(&self) -> &[u8] ⓘ
pub fn as_sensitive_bytes(&self) -> &[u8] ⓘ
Borrows the secret bytes as a slice.
Sourcepub fn exposed_in_as_str<T, Z, FN>(&self, f: FN) -> T
pub fn exposed_in_as_str<T, Z, FN>(&self, f: FN) -> T
Lends a String copy of the secret to f and returns the first item of
its result.
f returns a pair (T, Z): T is the result, Z is whatever must be
zeroized once f returns, usually the String it was given. A copy
that f neither returns as Z nor zeroizes itself is not zeroized.
exposed_in_as_zstr needs no such pair.
If f panics, the copy is freed without being zeroized. Use
exposed_in_as_zstr, which zeroizes it on
unwinding too.
§Panics
Panics if the bytes are not valid UTF-8.
use secret_vault_value::SecretValue;
let secret_value = SecretValue::from("my-password");
let length = secret_value.exposed_in_as_str(|password| (password.len(), password));
assert_eq!(length, 11);Sourcepub fn exposed_in_as_zstr<T, FN>(&self, f: FN) -> T
pub fn exposed_in_as_zstr<T, FN>(&self, f: FN) -> T
Lends a Zeroizing<String> copy of the secret to f and returns its
result.
The copy is zeroized when f drops it.
§Panics
Panics if the bytes are not valid UTF-8.
use secret_vault_value::SecretValue;
let secret_value = SecretValue::from("my-password");
let length = secret_value.exposed_in_as_zstr(|password| password.len());
assert_eq!(length, 11);Sourcepub fn exposed_in_as_vec<T, Z, FN>(&self, f: FN) -> T
pub fn exposed_in_as_vec<T, Z, FN>(&self, f: FN) -> T
Lends a Vec<u8> copy of the secret to f and returns the first item
of its result.
The (T, Z) pair works as in
exposed_in_as_str: Z is zeroized once
f returns.
If f panics, the copy is freed without being zeroized. Use
exposed_in_as_zvec, which zeroizes it on
unwinding too.
use secret_vault_value::SecretValue;
let secret_value = SecretValue::new(vec![1, 2, 3]);
let sum = secret_value.exposed_in_as_vec(|bytes| (bytes.iter().sum::<u8>(), bytes));
assert_eq!(sum, 6);Sourcepub fn exposed_in_as_zvec<T, FN>(&self, f: FN) -> T
pub fn exposed_in_as_zvec<T, FN>(&self, f: FN) -> T
Lends a Zeroizing<Vec<u8>> copy of the secret to f and returns
its result.
The copy is zeroized when f drops it.
use secret_vault_value::SecretValue;
let secret_value = SecretValue::new(vec![1, 2, 3]);
let sum = secret_value.exposed_in_as_zvec(|bytes| bytes.iter().sum::<u8>());
assert_eq!(sum, 6);Sourcepub async fn exposed_in_as_str_async<T, Z, FN, FI>(&self, f: FN) -> T
pub async fn exposed_in_as_str_async<T, Z, FN, FI>(&self, f: FN) -> T
Async form of exposed_in_as_str: Z is
zeroized once the future returned by f completes.
If f or its future panics, or the future is dropped before it
completes (a timeout, a losing select! branch, an aborted task), the
copy is freed without being zeroized. Use
exposed_in_as_zstr_async, which
zeroizes it in those cases too.
§Panics
Panics if the bytes are not valid UTF-8.
use secret_vault_value::SecretValue;
async fn password_length(secret_value: &SecretValue) -> usize {
secret_value
.exposed_in_as_str_async(|password| async move { (password.len(), password) })
.await
}Sourcepub async fn exposed_in_as_zstr_async<T, FN, FI>(&self, f: FN) -> T
pub async fn exposed_in_as_zstr_async<T, FN, FI>(&self, f: FN) -> T
Async form of exposed_in_as_zstr: the
copy is zeroized when the future returned by f drops it.
§Panics
Panics if the bytes are not valid UTF-8.
use secret_vault_value::SecretValue;
async fn password_length(secret_value: &SecretValue) -> usize {
secret_value
.exposed_in_as_zstr_async(|password| async move { password.len() })
.await
}Sourcepub async fn exposed_in_as_vec_async<T, Z, FN, FI>(&self, f: FN) -> T
pub async fn exposed_in_as_vec_async<T, Z, FN, FI>(&self, f: FN) -> T
Async form of exposed_in_as_vec: Z is
zeroized once the future returned by f completes.
If f or its future panics, or the future is dropped before it
completes (a timeout, a losing select! branch, an aborted task), the
copy is freed without being zeroized. Use
exposed_in_as_zvec_async, which
zeroizes it in those cases too.
use secret_vault_value::SecretValue;
async fn key_length(secret_value: &SecretValue) -> usize {
secret_value
.exposed_in_as_vec_async(|key| async move { (key.len(), key) })
.await
}Sourcepub async fn exposed_in_as_zvec_async<T, FN, FI>(&self, f: FN) -> T
pub async fn exposed_in_as_zvec_async<T, FN, FI>(&self, f: FN) -> T
Async form of exposed_in_as_zvec: the
copy is zeroized when the future returned by f drops it.
use secret_vault_value::SecretValue;
async fn key_length(secret_value: &SecretValue) -> usize {
secret_value
.exposed_in_as_zvec_async(|key| async move { key.len() })
.await
}Source§impl SecretValue
impl SecretValue
Sourcepub fn expose_json_value_as<T>(&self) -> Result<T, Error>where
T: for<'de> Deserialize<'de> + for<'de> Zeroize,
pub fn expose_json_value_as<T>(&self) -> Result<T, Error>where
T: for<'de> Deserialize<'de> + for<'de> Zeroize,
Parses the secret as JSON into T.
The Zeroize bound lets the caller wipe the result, but the result is
not zeroized automatically: call zeroize() on it, or wrap it in
zeroize::Zeroizing, once it is no longer needed.
§Errors
Returns serde_json::Error if the secret is not valid JSON for T.
use secret_vault_value::SecretValue;
use serde::Deserialize;
use zeroize::{Zeroize, Zeroizing};
#[derive(Deserialize, Zeroize)]
struct Credentials {
user: String,
password: String,
}
let secret_value = SecretValue::from(r#"{"user":"admin","password":"my-password"}"#);
let credentials = Zeroizing::new(secret_value.expose_json_value_as::<Credentials>()?);
assert_eq!(credentials.user, "admin");Trait Implementations§
Source§impl Clone for SecretValue
impl Clone for SecretValue
Source§fn clone(&self) -> SecretValue
fn clone(&self) -> SecretValue
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SecretValue
impl Debug for SecretValue
Source§impl Default for SecretValue
impl Default for SecretValue
Source§fn default() -> SecretValue
fn default() -> SecretValue
Source§impl<'de> Deserialize<'de> for SecretValue
Deserializes the secret from a string or from bytes.
impl<'de> Deserialize<'de> for SecretValue
Deserializes the secret from a string or from bytes.
Source§fn deserialize<D>(
deserializer: D,
) -> Result<SecretValue, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<SecretValue, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Display for SecretValue
impl Display for SecretValue
Source§impl Drop for SecretValue
impl Drop for SecretValue
impl Eq for SecretValue
Source§impl From<&mut String> for SecretValue
Copies the bytes and zeroizes the caller’s String, leaving it empty.
impl From<&mut String> for SecretValue
Copies the bytes and zeroizes the caller’s String, leaving it empty.
Source§fn from(str: &mut String) -> SecretValue
fn from(str: &mut String) -> SecretValue
Source§impl From<&mut Vec<u8>> for SecretValue
Copies the bytes and zeroizes the caller’s Vec, leaving it empty.
impl From<&mut Vec<u8>> for SecretValue
Copies the bytes and zeroizes the caller’s Vec, leaving it empty.
Source§impl From<&str> for SecretValue
impl From<&str> for SecretValue
Source§fn from(str: &str) -> SecretValue
fn from(str: &str) -> SecretValue
Source§impl From<String> for SecretValue
Copies the bytes and zeroizes the source String.
impl From<String> for SecretValue
Copies the bytes and zeroizes the source String.
Source§fn from(str: String) -> SecretValue
fn from(str: String) -> SecretValue
Source§impl Hash for SecretValue
impl Hash for SecretValue
Source§impl PartialEq for SecretValue
impl PartialEq for SecretValue
Source§impl Serialize for SecretValue
Serializes the secret as a string.
impl Serialize for SecretValue
Serializes the secret as a string.
Fails if the bytes are not valid UTF-8.
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Auto Trait Implementations§
impl Freeze for SecretValue
impl RefUnwindSafe for SecretValue
impl Send for SecretValue
impl Sync for SecretValue
impl Unpin for SecretValue
impl UnsafeUnpin for SecretValue
impl UnwindSafe for SecretValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request