From 3b88cd1725bdded57c2ff845db5d471d2da63e6a Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 17 Nov 2020 15:20:48 +0100 Subject: openpgp: Require Write for impl Digest. --- openpgp/src/crypto/backend/cng/hash.rs | 12 ++++++++++++ openpgp/src/crypto/hash.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/openpgp/src/crypto/backend/cng/hash.rs b/openpgp/src/crypto/backend/cng/hash.rs index 1f58d4cc..8d3bbb0f 100644 --- a/openpgp/src/crypto/backend/cng/hash.rs +++ b/openpgp/src/crypto/backend/cng/hash.rs @@ -1,4 +1,5 @@ use core::convert::{TryFrom, TryInto}; +use std::io; use std::sync::Mutex; use crate::crypto::hash::Digest; @@ -61,6 +62,17 @@ impl Digest for Hash { } } +impl io::Write for Hash { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.update(buf); + Ok(buf.len()) + } + fn flush(&mut self) -> io::Result<()> { + // Do nothing. + Ok(()) + } +} + impl TryFrom for cng::HashAlgorithmId { type Error = Error; diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index 410d9eae..27641429 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -31,7 +31,7 @@ use std::io::{self, Write}; const DUMP_HASHED_VALUES: Option<&str> = None; /// Hasher capable of calculating a digest for the input byte stream. -pub(crate) trait Digest: DynClone + Send + Sync { +pub(crate) trait Digest: DynClone + Write + Send + Sync { /// Returns the algorithm. fn algo(&self) -> HashAlgorithm; @@ -229,6 +229,16 @@ impl Digest for HashDumper { } } +impl io::Write for HashDumper { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.hasher.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.hasher.flush() + } +} + /// Hashes OpenPGP packets and related types. /// /// Some OpenPGP data structures need to be hashed to be covered by -- cgit v1.2.3