pub struct SecureBytesWriter<'a> { /* private fields */ }Expand description
An io::Write that appends everything written to it into a SecureBytes.
Handing a secret to serde_json::to_string/to_vec leaves the plaintext in an
ordinary String/Vec that nothing zeroizes, and impl Serialize cannot wipe
that buffer for you — a Serialize impl only ever sees a generic
serde::Serializer. Building the serializer around this writer instead keeps the
only heap copy of the plaintext in memory that is locked while unused and zeroized
on drop. Growth cannot leave a stale copy behind either: SecureVec::reserve
zeroizes the old allocation after moving the elements.
§Example
use secure_types::{SecureBytes, SecureBytesWriter};
use std::io::Write;
let mut buffer = SecureBytes::new_with_capacity(32).unwrap();
write!(SecureBytesWriter::new(&mut buffer), "secret").unwrap();
buffer.unlock_slice(|bytes| assert_eq!(bytes, b"secret"));This writer targets SecureBytes. If you want the result as a
SecureString, follow up with SecureString::try_from,
which validates the UTF-8 in a single pass.
The codec feature’s encode writes its own format into locked memory; this writer is
for pointing some other serializer at locked memory.
Implementations§
Source§impl<'a> SecureBytesWriter<'a>
impl<'a> SecureBytesWriter<'a>
Sourcepub fn new(bytes: &'a mut SecureBytes) -> Self
pub fn new(bytes: &'a mut SecureBytes) -> Self
Wraps bytes, which receives everything written to the returned writer.
Trait Implementations§
Source§impl Write for SecureBytesWriter<'_>
impl Write for SecureBytesWriter<'_>
Source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Appends in one go, where the default implementation would call write
repeatedly and pay an mprotect pair per fragment.
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)