Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh-encoding: adds a DigestWriter #268

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ssh-encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ base64ct = { version = "1.4", optional = true }
bytes = { version = "1", optional = true, default-features = false }
pem-rfc7468 = { version = "1.0.0-rc.1", optional = true }
sha2 = { version = "=0.11.0-pre.4", optional = true, default-features = false }
digest = { version = "=0.11.0-pre.9", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
2 changes: 1 addition & 1 deletion ssh-encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub use crate::{
error::{Error, Result},
label::{Label, LabelError},
reader::Reader,
writer::Writer,
writer::{DigestWriter, Writer},
};

#[cfg(feature = "base64")]
Expand Down
23 changes: 11 additions & 12 deletions ssh-encoding/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use alloc::vec::Vec;
#[cfg(feature = "bytes")]
use bytes::{BufMut, BytesMut};

#[cfg(feature = "sha2")]
use sha2::{Digest, Sha256, Sha512};
use digest::Digest;

/// Writer trait which encodes the SSH binary format to various output
/// encodings.
Expand All @@ -34,18 +33,18 @@ impl Writer for BytesMut {
}
}

#[cfg(feature = "sha2")]
impl Writer for Sha256 {
fn write(&mut self, bytes: &[u8]) -> Result<()> {
self.update(bytes);
Ok(())
}
}
/// Wrapper for digests.
///
/// This allows to update digests from the serializer directly.
#[derive(Debug)]
pub struct DigestWriter<'d, D>(pub &'d mut D);

#[cfg(feature = "sha2")]
impl Writer for Sha512 {
impl<D> Writer for DigestWriter<'_, D>
where
D: Digest,
{
fn write(&mut self, bytes: &[u8]) -> Result<()> {
self.update(bytes);
self.0.update(bytes);
Ok(())
}
}
10 changes: 7 additions & 3 deletions ssh-key/src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::{
};
use encoding::{
base64::{Base64Unpadded, Encoding},
Encode,
DigestWriter, Encode,
};
use sha2::{Digest, Sha256, Sha512};

Expand Down Expand Up @@ -63,12 +63,16 @@ impl Fingerprint {
match algorithm {
HashAlg::Sha256 => {
let mut digest = Sha256::new();
public_key.encode(&mut digest).expect(FINGERPRINT_ERR_MSG);
public_key
.encode(&mut DigestWriter(&mut digest))
.expect(FINGERPRINT_ERR_MSG);
Self::Sha256(digest.finalize().into())
}
HashAlg::Sha512 => {
let mut digest = Sha512::new();
public_key.encode(&mut digest).expect(FINGERPRINT_ERR_MSG);
public_key
.encode(&mut DigestWriter(&mut digest))
.expect(FINGERPRINT_ERR_MSG);
Self::Sha512(digest.finalize().into())
}
}
Expand Down
Loading