From f61e93ccf1db7e7e3c9604fdb09b4e25195d88b2 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 17 Feb 2025 12:47:40 +1100 Subject: [PATCH] Properly deprecate Hash::from_slice The `hashes::error::FromSliceError` error is only returned from `from_slice`. We attempted to deprecate this function but it seems we only did half a job at it. - deprecate _all_ instances of the method/function - deprecate the error type --- hashes/src/hmac/mod.rs | 8 +++++--- hashes/src/internal_macros.rs | 7 +++++-- hashes/src/lib.rs | 13 +++++++++---- hashes/src/macros.rs | 6 ++++-- hashes/src/sha256t/mod.rs | 7 ++++--- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/hashes/src/hmac/mod.rs b/hashes/src/hmac/mod.rs index eace7f951..8215e48a7 100644 --- a/hashes/src/hmac/mod.rs +++ b/hashes/src/hmac/mod.rs @@ -12,7 +12,7 @@ use core::{convert, fmt, str}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::{FromSliceError, GeneralHash, Hash, HashEngine}; +use crate::{GeneralHash, Hash, HashEngine}; /// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -132,8 +132,10 @@ impl Hash for Hmac { fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) } - #[allow(deprecated)] - fn from_slice(sl: &[u8]) -> Result, FromSliceError> { T::from_slice(sl).map(Hmac) } + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + fn from_slice(sl: &[u8]) -> Result, crate::FromSliceError> { + T::from_slice(sl).map(Hmac) + } fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() } diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 67b9f6170..fe5d6aa71 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -42,7 +42,8 @@ macro_rules! hash_trait_impls { fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } - #[allow(deprecated)] + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + #[allow(deprecated)] // Because of `from_slice`. fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result, $crate::FromSliceError> { Self::from_slice(sl) } @@ -126,11 +127,13 @@ macro_rules! hash_type_no_default { } /// Copies a byte slice into a hash object. + #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] + #[allow(deprecated_in_future)] // Because of `FromSliceError`. pub fn from_slice( sl: &[u8], ) -> $crate::_export::_core::result::Result { if sl.len() != $bits / 8 { - Err($crate::FromSliceError($crate::error::FromSliceErrorInner { + Err($crate::error::FromSliceError($crate::error::FromSliceErrorInner { expected: $bits / 8, got: sl.len(), })) diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index f4fb6fd44..2b8d5924c 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -96,10 +96,11 @@ pub mod _export { } } +#[deprecated(since = "TBD", note = "unused now that `Hash::from_slice` is deprecated")] +mod error; mod internal_macros; pub mod cmp; -pub mod error; pub mod hash160; pub mod hkdf; pub mod hmac; @@ -132,11 +133,9 @@ use core::{convert, hash}; #[rustfmt::skip] // Keep public re-exports separate. #[doc(inline)] pub use self::{ - error::FromSliceError, hkdf::Hkdf, hmac::{Hmac, HmacEngine}, }; - /// HASH-160: Alias for the [`hash160::Hash`] hash type. #[doc(inline)] pub use hash160::Hash as Hash160; @@ -165,6 +164,11 @@ pub use sha512_256::Hash as Sha512_256; #[doc(inline)] pub use siphash24::Hash as Siphash24; +/// Attempted to create a hash from an invalid length slice. +#[deprecated(since = "TBD", note = "unused now that `Hash::from_slice` is deprecated")] +#[allow(deprecated_in_future)] +pub type FromSliceError = crate::error::FromSliceError; // Alias instead of re-export so we can deprecate it. + /// Tagged SHA-256: Type alias for the [`sha256t::Hash`] hash type. pub type Sha256t = sha256t::Hash; @@ -257,7 +261,8 @@ pub trait Hash: fn from_byte_array(bytes: Self::Bytes) -> Self; /// Copies a byte slice into a hash object. - #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + #[deprecated(since = "TBD", note = "use `from_byte_array` instead")] fn from_slice(sl: &[u8]) -> Result; /// Returns the underlying byte array. diff --git a/hashes/src/macros.rs b/hashes/src/macros.rs index 12ca01520..9371eb115 100644 --- a/hashes/src/macros.rs +++ b/hashes/src/macros.rs @@ -141,7 +141,8 @@ macro_rules! hash_newtype { /// Copies a byte slice into a hash object. #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] - #[allow(deprecated)] + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + #[allow(deprecated)] // Because of `from_slice`. pub fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> { Ok($newtype(<$hash as $crate::Hash>::from_slice(sl)?)) } @@ -178,7 +179,8 @@ macro_rules! hash_newtype { fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } #[inline] - #[allow(deprecated)] + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + #[allow(deprecated)] // Because of `from_slice`. fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> { Self::from_slice(sl) } diff --git a/hashes/src/sha256t/mod.rs b/hashes/src/sha256t/mod.rs index 5f4ce2fe1..56592820e 100644 --- a/hashes/src/sha256t/mod.rs +++ b/hashes/src/sha256t/mod.rs @@ -7,7 +7,7 @@ use core::marker::PhantomData; #[cfg(doc)] use crate::sha256::Midstate; -use crate::{sha256, FromSliceError, HashEngine as _}; +use crate::{sha256, HashEngine as _}; /// Trait representing a tag that can be used as a context for SHA256t hashes. pub trait Tag: Clone { @@ -44,11 +44,12 @@ where /// Copies a byte slice into a hash object. #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] - pub fn from_slice(sl: &[u8]) -> Result, FromSliceError> { + #[allow(deprecated_in_future)] // Because of `FromSliceError`. + pub fn from_slice(sl: &[u8]) -> Result, crate::FromSliceError> { use crate::error::FromSliceErrorInner; if sl.len() != 32 { - Err(FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() })) + Err(crate::error::FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() })) } else { let mut ret = [0; 32]; ret.copy_from_slice(sl);