Hasini Gunasinghe | ed1c290 | 2022-09-10 01:23:21 +0000 | [diff] [blame] | 1 | //! SPKI fingerprint support. |
| 2 | |
| 3 | use der::Writer; |
| 4 | use sha2::{Digest, Sha256}; |
| 5 | |
| 6 | /// Size of a SHA-256 SPKI fingerprint in bytes. |
| 7 | pub(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")))] |
| 16 | pub 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)] |
| 21 | pub(crate) struct Builder { |
| 22 | /// In-progress digest being computed from streaming DER. |
| 23 | digest: Sha256, |
| 24 | } |
| 25 | |
| 26 | impl 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 | |
| 38 | impl Writer for Builder { |
| 39 | fn write(&mut self, der_bytes: &[u8]) -> der::Result<()> { |
| 40 | self.digest.update(der_bytes); |
| 41 | Ok(()) |
| 42 | } |
| 43 | } |