1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use super::secret::SecretBuffer;
use crate::{DecodeError, DecodedBuffer, EncodedBuffer, STANDARD};
#[cfg(feature = "alloc")]
impl From<alloc::vec::Vec<u8>> for SecretBuffer {
/// Wraps an owned vector as sensitive material.
///
/// Spare capacity is cleared immediately before the vector is stored.
/// Use [`SecretBuffer::from_slice`] when the source data is borrowed.
fn from(bytes: alloc::vec::Vec<u8>) -> Self {
Self::from_vec(bytes)
}
}
#[cfg(feature = "alloc")]
impl From<alloc::string::String> for SecretBuffer {
/// Wraps an owned UTF-8 string as sensitive material.
///
/// The string is consumed without copying its initialized bytes. Spare
/// vector capacity is cleared immediately before the bytes are stored.
fn from(text: alloc::string::String) -> Self {
Self::from_vec(text.into_bytes())
}
}
#[cfg(feature = "alloc")]
impl<const CAP: usize> From<EncodedBuffer<CAP>> for SecretBuffer {
/// Copies visible encoded bytes from a stack-backed buffer into an owned
/// redacted buffer.
///
/// The consumed stack-backed buffer clears its backing array when it is
/// dropped at the end of the conversion.
fn from(buffer: EncodedBuffer<CAP>) -> Self {
Self::from_slice(buffer.as_bytes())
}
}
#[cfg(feature = "alloc")]
impl<const CAP: usize> From<DecodedBuffer<CAP>> for SecretBuffer {
/// Copies visible decoded bytes from a stack-backed buffer into an owned
/// redacted buffer.
///
/// The consumed stack-backed buffer clears its backing array when it is
/// dropped at the end of the conversion.
fn from(buffer: DecodedBuffer<CAP>) -> Self {
Self::from_slice(buffer.as_bytes())
}
}
#[cfg(feature = "alloc")]
impl TryFrom<&[u8]> for SecretBuffer {
type Error = DecodeError;
/// Decodes strict standard padded Base64 into a redacted owned buffer.
///
/// Use [`crate::Engine::decode_secret`] or [`crate::Profile::decode_secret`]
/// when a different alphabet, padding mode, or line-wrapping profile is
/// required. These conversions always use [`crate::STANDARD`]; URL-safe,
/// bcrypt, crypt, MIME, PEM, and custom alphabets must use an explicit
/// engine or profile.
///
/// # Security
///
/// This idiomatic conversion uses the strict standard decoder, not the
/// constant-time-oriented decoder. It may branch or return early on
/// malformed input and reports exact [`DecodeError`] positions. For
/// secret-bearing tokens or key material where malformed-input timing
/// matters, use [`crate::ct::CtEngine::decode_secret`] through
/// [`crate::ct::STANDARD`], or use staged decode and then wrap the
/// successful output in `SecretBuffer`.
fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
STANDARD.decode_secret(input)
}
}
#[cfg(feature = "alloc")]
impl<const N: usize> TryFrom<&[u8; N]> for SecretBuffer {
type Error = DecodeError;
/// Decodes a strict standard padded Base64 byte array into a redacted
/// owned buffer.
///
/// Use [`crate::Engine::decode_secret`] or [`crate::Profile::decode_secret`]
/// when a different alphabet, padding mode, or line-wrapping profile is
/// required. These conversions always use [`crate::STANDARD`]; URL-safe,
/// bcrypt, crypt, MIME, PEM, and custom alphabets must use an explicit
/// engine or profile.
///
/// # Security
///
/// This idiomatic conversion uses the strict standard decoder, not the
/// constant-time-oriented decoder. It may branch or return early on
/// malformed input and reports exact [`DecodeError`] positions. For
/// secret-bearing tokens or key material where malformed-input timing
/// matters, use [`crate::ct::CtEngine::decode_secret`] through
/// [`crate::ct::STANDARD`], or use staged decode and then wrap the
/// successful output in `SecretBuffer`.
fn try_from(input: &[u8; N]) -> Result<Self, Self::Error> {
Self::try_from(&input[..])
}
}
#[cfg(feature = "alloc")]
impl TryFrom<&str> for SecretBuffer {
type Error = DecodeError;
/// Decodes strict standard padded Base64 text into a redacted owned buffer.
///
/// Use [`crate::Engine::decode_secret`] or [`crate::Profile::decode_secret`]
/// when a different alphabet, padding mode, or line-wrapping profile is
/// required. These conversions always use [`crate::STANDARD`]; URL-safe,
/// bcrypt, crypt, MIME, PEM, and custom alphabets must use an explicit
/// engine or profile.
///
/// # Security
///
/// This idiomatic conversion uses the strict standard decoder, not the
/// constant-time-oriented decoder. It may branch or return early on
/// malformed input and reports exact [`DecodeError`] positions. For
/// secret-bearing tokens or key material where malformed-input timing
/// matters, use [`crate::ct::CtEngine::decode_secret`] through
/// [`crate::ct::STANDARD`], or use staged decode and then wrap the
/// successful output in `SecretBuffer`.
fn try_from(input: &str) -> Result<Self, Self::Error> {
Self::try_from(input.as_bytes())
}
}
#[cfg(feature = "alloc")]
impl core::str::FromStr for SecretBuffer {
type Err = DecodeError;
/// Decodes strict standard padded Base64 text into a redacted owned buffer.
///
/// Use [`crate::Engine::decode_secret`] or [`crate::Profile::decode_secret`]
/// when a different alphabet, padding mode, or line-wrapping profile is
/// required. These conversions always use [`crate::STANDARD`]; URL-safe,
/// bcrypt, crypt, MIME, PEM, and custom alphabets must use an explicit
/// engine or profile.
///
/// # Security
///
/// This idiomatic conversion uses the strict standard decoder, not the
/// constant-time-oriented decoder. It may branch or return early on
/// malformed input and reports exact [`DecodeError`] positions. For
/// secret-bearing tokens or key material where malformed-input timing
/// matters, use [`crate::ct::CtEngine::decode_secret`] through
/// [`crate::ct::STANDARD`], or use staged decode and then wrap the
/// successful output in `SecretBuffer`.
fn from_str(input: &str) -> Result<Self, Self::Err> {
Self::try_from(input)
}
}