Skip to content

Commit 7f63ffb

Browse files
authored
spki: adds a der::Writer adapter for Digest (#1769)
1 parent cb1af5b commit 7f63ffb

File tree

6 files changed

+42
-50
lines changed

6 files changed

+42
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spki/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ der = { version = "0.8.0-rc.0", features = ["oid"] }
2121
# Optional dependencies
2222
arbitrary = { version = "1.4", features = ["derive"], optional = true }
2323
base64ct = { version = "1", optional = true, default-features = false }
24+
digest = { version = "0.11.0-pre.10", optional = true, default-features = false }
2425
sha2 = { version = "=0.11.0-pre.5", optional = true, default-features = false }
2526

2627
[dev-dependencies]
@@ -33,7 +34,7 @@ std = ["der/std", "alloc"]
3334

3435
arbitrary = ["std", "dep:arbitrary", "der/arbitrary"]
3536
base64 = ["dep:base64ct"]
36-
fingerprint = ["sha2"]
37+
fingerprint = ["digest", "sha2"]
3738
pem = ["alloc", "der/pem"]
3839

3940
[package.metadata.docs.rs]

spki/src/digest.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use der::{Result, Writer};
2+
use digest::Digest;
3+
4+
/// Adapter object to write to a digest backend
5+
pub struct DigestWriter<'d, D>(pub &'d mut D);
6+
7+
impl<D> Writer for DigestWriter<'_, D>
8+
where
9+
D: Digest,
10+
{
11+
fn write(&mut self, slice: &[u8]) -> Result<()> {
12+
self.0.update(slice);
13+
Ok(())
14+
}
15+
}

spki/src/fingerprint.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

spki/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ mod error;
4343
mod spki;
4444
mod traits;
4545

46-
#[cfg(feature = "fingerprint")]
47-
mod fingerprint;
46+
#[cfg(feature = "digest")]
47+
mod digest;
4848

4949
pub use crate::{
5050
algorithm::{AlgorithmIdentifier, AlgorithmIdentifierRef, AlgorithmIdentifierWithOid},
@@ -67,5 +67,18 @@ pub use {
6767
der::Document,
6868
};
6969

70+
#[cfg(feature = "digest")]
71+
pub use crate::digest::DigestWriter;
72+
73+
/// Size of a SHA-256 SPKI fingerprint in bytes.
74+
#[cfg(feature = "fingerprint")]
75+
pub(crate) const SIZE: usize = 32;
76+
77+
/// Raw bytes of a SPKI fingerprint i.e. SHA-256 digest of
78+
/// `SubjectPublicKeyInfo`'s DER encoding.
79+
///
80+
/// See [RFC7469 § 2.1.1] for more information.
81+
///
82+
/// [RFC7469 § 2.1.1]: https://datatracker.ietf.org/doc/html/rfc7469#section-2.1.1
7083
#[cfg(feature = "fingerprint")]
71-
pub use crate::fingerprint::FingerprintBytes;
84+
pub type FingerprintBytes = [u8; SIZE];

spki/src/spki.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ use der::{
1515
};
1616

1717
#[cfg(feature = "fingerprint")]
18-
use crate::{FingerprintBytes, fingerprint};
18+
use {
19+
crate::{DigestWriter, FingerprintBytes},
20+
digest::Digest,
21+
sha2::Sha256,
22+
};
1923

2024
#[cfg(feature = "pem")]
2125
use der::pem::PemLabel;
@@ -80,9 +84,9 @@ where
8084
/// [RFC7469 § 2.1.1]: https://datatracker.ietf.org/doc/html/rfc7469#section-2.1.1
8185
#[cfg(feature = "fingerprint")]
8286
pub fn fingerprint_bytes(&self) -> Result<FingerprintBytes> {
83-
let mut builder = fingerprint::Builder::new();
84-
self.encode(&mut builder)?;
85-
Ok(builder.finish())
87+
let mut hash = Sha256::new();
88+
self.encode(&mut DigestWriter(&mut hash))?;
89+
Ok(hash.finalize().into())
8690
}
8791
}
8892

0 commit comments

Comments
 (0)