From eb097fd2fc91a6fb9c04913828fc43090e3e8294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 4 Apr 2025 15:30:48 +0300 Subject: [PATCH 1/4] digest: move `std::io` stuff to `digest-io` --- crypto/Cargo.toml | 1 - digest/Cargo.toml | 1 - digest/src/core_api/rt_variable.rs | 14 --- digest/src/core_api/wrapper.rs | 14 --- digest/src/core_api/xof_reader.rs | 12 --- digest/src/hashreader.rs | 136 ----------------------------- digest/src/hashwriter.rs | 121 ------------------------- digest/src/lib.rs | 12 --- 8 files changed, 311 deletions(-) delete mode 100644 digest/src/hashreader.rs delete mode 100644 digest/src/hashwriter.rs diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index 6aa12dd93..28782c641 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -26,7 +26,6 @@ universal-hash = { version = "0.6.0-rc.0", path = "../universal-hash", optional [features] std = [ - "digest/std", "elliptic-curve/std", "signature/std", ] diff --git a/digest/Cargo.toml b/digest/Cargo.toml index 9f277b796..7f41d294e 100644 --- a/digest/Cargo.toml +++ b/digest/Cargo.toml @@ -31,7 +31,6 @@ os_rng = ["crypto-common/rand_core", "rand_core"] oid = ["const-oid"] zeroize = ["dep:zeroize", "block-buffer?/zeroize"] alloc = [] -std = ["alloc"] dev = ["blobby"] [package.metadata.docs.rs] diff --git a/digest/src/core_api/rt_variable.rs b/digest/src/core_api/rt_variable.rs index 17f13ae8e..1cb212ea9 100644 --- a/digest/src/core_api/rt_variable.rs +++ b/digest/src/core_api/rt_variable.rs @@ -184,17 +184,3 @@ where }) } } - -#[cfg(feature = "std")] -impl std::io::Write for RtVariableCoreWrapper { - #[inline] - fn write(&mut self, buf: &[u8]) -> std::io::Result { - Update::update(self, buf); - Ok(buf.len()) - } - - #[inline] - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } -} diff --git a/digest/src/core_api/wrapper.rs b/digest/src/core_api/wrapper.rs index 55062e7c8..8d4cf6413 100644 --- a/digest/src/core_api/wrapper.rs +++ b/digest/src/core_api/wrapper.rs @@ -220,20 +220,6 @@ where } } -#[cfg(feature = "std")] -impl std::io::Write for CoreWrapper { - #[inline] - fn write(&mut self, buf: &[u8]) -> std::io::Result { - Update::update(self, buf); - Ok(buf.len()) - } - - #[inline] - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } -} - /// A proxy trait to a core type implemented by [`CoreWrapper`] // TODO: replace with an inherent associated type on stabilization: // https://github.com/rust-lang/rust/issues/8995 diff --git a/digest/src/core_api/xof_reader.rs b/digest/src/core_api/xof_reader.rs index 0c832df0c..0bb63933c 100644 --- a/digest/src/core_api/xof_reader.rs +++ b/digest/src/core_api/xof_reader.rs @@ -36,18 +36,6 @@ where } } -#[cfg(feature = "std")] -impl std::io::Read for XofReaderCoreWrapper -where - T: XofReaderCore, -{ - #[inline] - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { - XofReader::read(self, buf); - Ok(buf.len()) - } -} - impl Drop for XofReaderCoreWrapper { #[inline] fn drop(&mut self) { diff --git a/digest/src/hashreader.rs b/digest/src/hashreader.rs deleted file mode 100644 index eaca370b4..000000000 --- a/digest/src/hashreader.rs +++ /dev/null @@ -1,136 +0,0 @@ -//! Adds hashing to any reader -use super::{Digest, FixedOutputReset, Output, Reset}; -use std::io; - -/// Abstraction over a reader which hashes the data being read -#[derive(Debug)] -pub struct HashReader { - reader: R, - hasher: D, -} - -impl HashReader { - /// Construct a new `HashReader` given an existing `reader` by value. - pub fn new(reader: R) -> Self { - Self::new_from_parts(D::new(), reader) - } - - /// Construct a new `HashReader` given an existing `hasher` and `reader` by value. - pub fn new_from_parts(hasher: D, reader: R) -> Self { - HashReader { reader, hasher } - } - - /// Replace the reader with another reader - pub fn replace_reader(&mut self, reader: R) { - self.reader = reader; - } - - /// Gets a reference to the underlying hasher - pub fn get_hasher(&self) -> &D { - &self.hasher - } - - /// Gets a reference to the underlying reader - pub fn get_reader(&self) -> &R { - &self.reader - } - - /// Gets a mutable reference to the underlying hasher - pub fn get_hasher_mut(&mut self) -> &mut D { - &mut self.hasher - } - - /// Gets a mutable reference to the underlying reader - /// Direct reads from the underlying reader are not hashed - pub fn get_reader_mut(&mut self) -> &mut R { - &mut self.reader - } - - /// Consume the HashReader and return its hasher - pub fn into_hasher(self) -> D { - self.hasher - } - - /// Consume the HashReader and return its internal reader - pub fn into_inner_reader(self) -> R { - self.reader - } - - /// Consume the HashReader and return its hasher and internal reader - pub fn into_parts(self) -> (D, R) { - (self.hasher, self.reader) - } - - /// Retrieve result and consume HashReader instance. - pub fn finalize(self) -> Output { - self.hasher.finalize() - } - - /// Write result into provided array and consume the HashReader instance. - pub fn finalize_into(self, out: &mut Output) { - self.hasher.finalize_into(out) - } - - /// Get output size of the hasher - pub fn output_size() -> usize { - ::output_size() - } -} - -impl Clone for HashReader { - fn clone(&self) -> HashReader { - HashReader { - reader: self.reader.clone(), - hasher: self.hasher.clone(), - } - } -} - -impl io::Read for HashReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let bytes = self.reader.read(buf)?; - - if bytes > 0 { - self.hasher.update(&buf[0..bytes]); - } - - Ok(bytes) - } -} - -impl HashReader { - /// Retrieve result and reset hasher instance. - pub fn finalize_reset(&mut self) -> Output { - Digest::finalize_reset(&mut self.hasher) - } - - /// Rrite result into provided array and reset the hasher instance. - pub fn finalize_into_reset(&mut self, out: &mut Output) { - Digest::finalize_into_reset(&mut self.hasher, out) - } -} -impl Reset for HashReader { - fn reset(&mut self) { - Digest::reset(&mut self.hasher) - } -} - -impl HashReader { - /// Read and hash all bytes remaining in the reader, discarding the data - /// Based on implementation in b2sum crate, MIT License Copyright (c) 2017 John Downey - pub fn hash_to_end(&mut self) { - loop { - let count = { - let data = self.reader.fill_buf().unwrap(); - if data.is_empty() { - break; - } - - self.hasher.update(data); - data.len() - }; - - self.reader.consume(count); - } - } -} diff --git a/digest/src/hashwriter.rs b/digest/src/hashwriter.rs deleted file mode 100644 index 354590385..000000000 --- a/digest/src/hashwriter.rs +++ /dev/null @@ -1,121 +0,0 @@ -//! Adds hashing to any writer. Inspired by implementation in phase2 crate. -use super::{Digest, FixedOutputReset, Output, Reset}; -use std::io; - -/// Abstraction over a writer which hashes the data being written. -#[derive(Debug)] -pub struct HashWriter { - writer: W, - hasher: D, -} - -impl HashWriter { - /// Construct a new `HashWriter` given an existing `writer` by value. - pub fn new(writer: W) -> Self { - Self::new_from_parts(D::new(), writer) - } - - /// Construct a new `HashWriter` given an existing `hasher` and `writer` by value. - pub fn new_from_parts(hasher: D, writer: W) -> Self { - HashWriter { writer, hasher } - } - - /// Replace the writer with another writer - pub fn replace_writer(&mut self, writer: W) { - self.writer = writer; - } - - /// Gets a reference to the underlying hasher - pub fn get_hasher(&self) -> &D { - &self.hasher - } - - /// Gets a reference to the underlying writer - pub fn get_writer(&self) -> &W { - &self.writer - } - - /// Gets a mutable reference to the underlying hasher - /// Updates to the digest are not written to the underlying writer - pub fn get_hasher_mut(&mut self) -> &mut D { - &mut self.hasher - } - - /// Gets a mutable reference to the underlying writer - /// Direct writes to the underlying writer are not hashed - pub fn get_writer_mut(&mut self) -> &mut W { - &mut self.writer - } - - /// Consume the HashWriter and return its hasher - pub fn into_hasher(self) -> D { - self.hasher - } - - /// Consume the HashWriter and return its internal writer - pub fn into_inner_writer(self) -> W { - self.writer - } - - /// Consume the HashWriter and return its hasher and internal writer - pub fn into_parts(self) -> (D, W) { - (self.hasher, self.writer) - } - - /// Retrieve result and consume HashWriter instance. - pub fn finalize(self) -> Output { - self.hasher.finalize() - } - - /// Write result into provided array and consume the HashWriter instance. - pub fn finalize_into(self, out: &mut Output) { - self.hasher.finalize_into(out) - } - - /// Get output size of the hasher - pub fn output_size() -> usize { - ::output_size() - } -} - -impl Clone for HashWriter { - fn clone(&self) -> HashWriter { - HashWriter { - writer: self.writer.clone(), - hasher: self.hasher.clone(), - } - } -} - -impl io::Write for HashWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - let bytes = self.writer.write(buf)?; - - if bytes > 0 { - self.hasher.update(&buf[0..bytes]); - } - - Ok(bytes) - } - - fn flush(&mut self) -> io::Result<()> { - self.writer.flush() - } -} - -impl HashWriter { - /// Retrieve result and reset hasher instance. - pub fn finalize_reset(&mut self) -> Output { - Digest::finalize_reset(&mut self.hasher) - } - - /// Write result into provided array and reset the hasher instance. - pub fn finalize_into_reset(&mut self, out: &mut Output) { - Digest::finalize_into_reset(&mut self.hasher, out) - } -} -impl Reset for HashWriter { - fn reset(&mut self) { - Digest::reset(&mut self.hasher) - } -} diff --git a/digest/src/lib.rs b/digest/src/lib.rs index bd9f3c035..d53a8afba 100644 --- a/digest/src/lib.rs +++ b/digest/src/lib.rs @@ -35,9 +35,6 @@ #[macro_use] extern crate alloc; -#[cfg(feature = "std")] -extern crate std; - #[cfg(feature = "rand_core")] pub use crypto_common::rand_core; @@ -303,12 +300,3 @@ impl fmt::Display for InvalidBufferSize { } impl core::error::Error for InvalidBufferSize {} - -#[cfg(feature = "std")] -mod hashwriter; -#[cfg(feature = "std")] -pub use hashwriter::HashWriter; -#[cfg(feature = "std")] -mod hashreader; -#[cfg(feature = "std")] -pub use hashreader::HashReader; From 638c936f52c9d910bfcf5a93a38a651d03555e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 4 Apr 2025 15:32:26 +0300 Subject: [PATCH 2/4] Fix CI --- .github/workflows/digest.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/digest.yml b/.github/workflows/digest.yml index e2f6e0eb0..f89b13c5d 100644 --- a/.github/workflows/digest.yml +++ b/.github/workflows/digest.yml @@ -60,5 +60,4 @@ jobs: - run: cargo test - run: cargo test --features dev - run: cargo test --features alloc - - run: cargo test --features std - run: cargo test --all-features From 4bd5788f4c4ed53d328235da51921b296a1ac5f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 4 Apr 2025 15:34:35 +0300 Subject: [PATCH 3/4] Add changelog entry --- digest/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/digest/CHANGELOG.md b/digest/CHANGELOG.md index a6c58234d..8af2a4d3d 100644 --- a/digest/CHANGELOG.md +++ b/digest/CHANGELOG.md @@ -15,10 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - `Mac::new`, `Mac::new_from_slice`, and `Mac::generate_key` methods ([#1173]) +- `HashReader` and `HashWriter` are moved to the `digest-io` crate ([#1809]) +- Removed `io::Write/Read` implementations in favor of the `digest_io::IoWrapper` type ([#1809]) [#1173]: https://github.com/RustCrypto/traits/pull/1173 [#1334]: https://github.com/RustCrypto/traits/pull/1334 [#1759]: https://github.com/RustCrypto/traits/pull/1759 +[#1809]: https://github.com/RustCrypto/traits/pull/1809 ## 0.10.7 (2023-05-19) ### Changed From 4cb38092cfe026f7361bc7fe75a27674144ea87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 4 Apr 2025 15:40:07 +0300 Subject: [PATCH 4/4] Update crate docs --- digest/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/digest/src/lib.rs b/digest/src/lib.rs index d53a8afba..e323f3640 100644 --- a/digest/src/lib.rs +++ b/digest/src/lib.rs @@ -18,9 +18,12 @@ //! Usually they should not be used in application-level code. //! //! Additionally hash functions implement traits from the standard library: -//! [`Default`], [`Clone`], [`Write`][std::io::Write]. The latter is -//! feature-gated behind `std` feature, which is usually enabled by default -//! by hash implementation crates. +//! [`Default`] and [`Clone`]. +//! +//! This crate does not provide any implementations of the `io::Read/Write` traits, +//! see the [`digest-io`] crate for `std::io`-compatibility wrappers. +//! +//! [`digest-io`]: https://docs.rs/digest-io #![no_std] #![cfg_attr(docsrs, feature(doc_auto_cfg))]