blob: 6a3901fb14f643b671efa127a7c50ad1e80e9503 [file] [log] [blame]
Hasini Gunasingheed1c2902022-09-10 01:23:21 +00001//! SPKI fingerprint support.
2
3use der::Writer;
4use sha2::{Digest, Sha256};
5
6/// Size of a SHA-256 SPKI fingerprint in bytes.
7pub(crate) const SIZE: usize = 32;
8
9/// Raw bytes of a SPKI fingerprint i.e. SHA-256 digest of
10/// `SubjectPublicKeyInfo`'s DER encoding.
11///
12/// See [RFC7469 § 2.1.1] for more information.
13///
14/// [RFC7469 § 2.1.1]: https://datatracker.ietf.org/doc/html/rfc7469#section-2.1.1
15#[cfg_attr(docsrs, doc(cfg(feature = "fingerprint")))]
16pub type FingerprintBytes = [u8; SIZE];
17
18/// Writer newtype which accepts DER being serialized on-the-fly and computes a
19/// hash of the contents.
20#[derive(Clone, Default)]
21pub(crate) struct Builder {
22 /// In-progress digest being computed from streaming DER.
23 digest: Sha256,
24}
25
26impl Builder {
27 /// Create a new fingerprint builder.
28 pub fn new() -> Self {
29 Self::default()
30 }
31
32 /// Finish computing a fingerprint, returning the computed digest.
33 pub fn finish(self) -> FingerprintBytes {
34 self.digest.finalize().into()
35 }
36}
37
38impl Writer for Builder {
39 fn write(&mut self, der_bytes: &[u8]) -> der::Result<()> {
40 self.digest.update(der_bytes);
41 Ok(())
42 }
43}