ssh_key/
writer.rs
1use 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#[cfg(feature = "alloc")]
16#[allow(clippy::integer_arithmetic)]
17pub(crate) fn base64_len(input_len: usize) -> usize {
18 (((input_len * 4) / 3) + 3) & !3
20}
21
22pub(crate) type Base64Writer<'o> = base64ct::Encoder<'o, base64ct::Base64>;
24
25pub(crate) trait Writer: Sized {
28 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}