ssh_encoding/base64/
writer.rs

1//! Base64 writer support (constant-time).
2
3use crate::{Result, Writer};
4
5/// Inner constant-time Base64 reader type from the `base64ct` crate.
6type Inner<'o> = base64ct::Encoder<'o, base64ct::Base64>;
7
8/// Constant-time Base64 writer implementation.
9pub struct Base64Writer<'o> {
10    inner: Inner<'o>,
11}
12
13impl<'o> Base64Writer<'o> {
14    /// Create a new Base64 writer which writes output to the given byte slice.
15    ///
16    /// Output constructed using this method is not line-wrapped.
17    pub fn new(output: &'o mut [u8]) -> Result<Self> {
18        Ok(Self {
19            inner: Inner::new(output)?,
20        })
21    }
22
23    /// Finish encoding data, returning the resulting Base64 as a `str`.
24    pub fn finish(self) -> Result<&'o str> {
25        Ok(self.inner.finish()?)
26    }
27}
28
29impl Writer for Base64Writer<'_> {
30    fn write(&mut self, bytes: &[u8]) -> Result<()> {
31        Ok(self.inner.encode(bytes)?)
32    }
33}