ssh_key/
writer.rs

1//! Writer trait and associated implementations.
2
3use crate::Result;
4use pem_rfc7468 as pem;
5
6#[cfg(feature = "alloc")]
7use alloc::vec::Vec;
8
9#[cfg(feature = "fingerprint")]
10use sha2::{Digest, Sha256, Sha512};
11
12/// Get the estimated length of data when encoded as Base64.
13///
14/// This is an upper bound where the actual length might be slightly shorter.
15#[cfg(feature = "alloc")]
16#[allow(clippy::integer_arithmetic)]
17pub(crate) fn base64_len(input_len: usize) -> usize {
18    // TODO(tarcieri): checked arithmetic
19    (((input_len * 4) / 3) + 3) & !3
20}
21
22/// Constant-time Base64 writer implementation.
23pub(crate) type Base64Writer<'o> = base64ct::Encoder<'o, base64ct::Base64>;
24
25/// Writer trait which encodes the SSH binary format to various output
26/// encodings.
27pub(crate) trait Writer: Sized {
28    /// Write the given bytes to the writer.
29    fn write(&mut self, bytes: &[u8]) -> Result<()>;
30}
31
32impl Writer for Base64Writer<'_> {
33    fn write(&mut self, bytes: &[u8]) -> Result<()> {
34        Ok(self.encode(bytes)?)
35    }
36}
37
38impl Writer for pem::Encoder<'_, '_> {
39    fn write(&mut self, bytes: &[u8]) -> Result<()> {
40        Ok(self.encode(bytes)?)
41    }
42}
43
44#[cfg(feature = "alloc")]
45#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
46impl Writer for Vec<u8> {
47    fn write(&mut self, bytes: &[u8]) -> Result<()> {
48        self.extend_from_slice(bytes);
49        Ok(())
50    }
51}
52
53#[cfg(feature = "fingerprint")]
54impl Writer for Sha256 {
55    fn write(&mut self, bytes: &[u8]) -> Result<()> {
56        self.update(bytes);
57        Ok(())
58    }
59}
60
61#[cfg(feature = "fingerprint")]
62impl Writer for Sha512 {
63    fn write(&mut self, bytes: &[u8]) -> Result<()> {
64        self.update(bytes);
65        Ok(())
66    }
67}