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
This commit is contained in:
Tobin C. Harding 2025-02-17 12:47:40 +11:00
parent 50c0af7138
commit f61e93ccf1
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
5 changed files with 27 additions and 14 deletions

View File

@ -12,7 +12,7 @@ use core::{convert, fmt, str};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer}; 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. /// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -132,8 +132,10 @@ impl<T: GeneralHash> Hash for Hmac<T> {
fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) } fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) }
#[allow(deprecated)] #[allow(deprecated_in_future)] // Because of `FromSliceError`.
fn from_slice(sl: &[u8]) -> Result<Hmac<T>, FromSliceError> { T::from_slice(sl).map(Hmac) } fn from_slice(sl: &[u8]) -> Result<Hmac<T>, crate::FromSliceError> {
T::from_slice(sl).map(Hmac)
}
fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() } fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() }

View File

@ -42,7 +42,8 @@ macro_rules! hash_trait_impls {
fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } 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<Hash<$($gen),*>, $crate::FromSliceError> { fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<Hash<$($gen),*>, $crate::FromSliceError> {
Self::from_slice(sl) Self::from_slice(sl)
} }
@ -126,11 +127,13 @@ macro_rules! hash_type_no_default {
} }
/// Copies a byte slice into a hash object. /// 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( pub fn from_slice(
sl: &[u8], sl: &[u8],
) -> $crate::_export::_core::result::Result<Hash, $crate::FromSliceError> { ) -> $crate::_export::_core::result::Result<Hash, $crate::FromSliceError> {
if sl.len() != $bits / 8 { if sl.len() != $bits / 8 {
Err($crate::FromSliceError($crate::error::FromSliceErrorInner { Err($crate::error::FromSliceError($crate::error::FromSliceErrorInner {
expected: $bits / 8, expected: $bits / 8,
got: sl.len(), got: sl.len(),
})) }))

View File

@ -96,10 +96,11 @@ pub mod _export {
} }
} }
#[deprecated(since = "TBD", note = "unused now that `Hash::from_slice` is deprecated")]
mod error;
mod internal_macros; mod internal_macros;
pub mod cmp; pub mod cmp;
pub mod error;
pub mod hash160; pub mod hash160;
pub mod hkdf; pub mod hkdf;
pub mod hmac; pub mod hmac;
@ -132,11 +133,9 @@ use core::{convert, hash};
#[rustfmt::skip] // Keep public re-exports separate. #[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)] #[doc(inline)]
pub use self::{ pub use self::{
error::FromSliceError,
hkdf::Hkdf, hkdf::Hkdf,
hmac::{Hmac, HmacEngine}, hmac::{Hmac, HmacEngine},
}; };
/// HASH-160: Alias for the [`hash160::Hash`] hash type. /// HASH-160: Alias for the [`hash160::Hash`] hash type.
#[doc(inline)] #[doc(inline)]
pub use hash160::Hash as Hash160; pub use hash160::Hash as Hash160;
@ -165,6 +164,11 @@ pub use sha512_256::Hash as Sha512_256;
#[doc(inline)] #[doc(inline)]
pub use siphash24::Hash as Siphash24; 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. /// Tagged SHA-256: Type alias for the [`sha256t::Hash`] hash type.
pub type Sha256t<T> = sha256t::Hash<T>; pub type Sha256t<T> = sha256t::Hash<T>;
@ -257,7 +261,8 @@ pub trait Hash:
fn from_byte_array(bytes: Self::Bytes) -> Self; fn from_byte_array(bytes: Self::Bytes) -> Self;
/// Copies a byte slice into a hash object. /// 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<Self, FromSliceError>; fn from_slice(sl: &[u8]) -> Result<Self, FromSliceError>;
/// Returns the underlying byte array. /// Returns the underlying byte array.

View File

@ -141,7 +141,8 @@ macro_rules! hash_newtype {
/// Copies a byte slice into a hash object. /// Copies a byte slice into a hash object.
#[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] #[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> { pub fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> {
Ok($newtype(<$hash as $crate::Hash>::from_slice(sl)?)) 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) } fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) }
#[inline] #[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> { fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> {
Self::from_slice(sl) Self::from_slice(sl)
} }

View File

@ -7,7 +7,7 @@ use core::marker::PhantomData;
#[cfg(doc)] #[cfg(doc)]
use crate::sha256::Midstate; 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. /// Trait representing a tag that can be used as a context for SHA256t hashes.
pub trait Tag: Clone { pub trait Tag: Clone {
@ -44,11 +44,12 @@ where
/// Copies a byte slice into a hash object. /// Copies a byte slice into a hash object.
#[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")]
pub fn from_slice(sl: &[u8]) -> Result<Hash<T>, FromSliceError> { #[allow(deprecated_in_future)] // Because of `FromSliceError`.
pub fn from_slice(sl: &[u8]) -> Result<Hash<T>, crate::FromSliceError> {
use crate::error::FromSliceErrorInner; use crate::error::FromSliceErrorInner;
if sl.len() != 32 { if sl.len() != 32 {
Err(FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() })) Err(crate::error::FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() }))
} else { } else {
let mut ret = [0; 32]; let mut ret = [0; 32];
ret.copy_from_slice(sl); ret.copy_from_slice(sl);