31
31
//! # #[cfg(all(feature = "rand-std", feature = "hashes-std"))] {
32
32
//! use secp256k1::rand::rngs::OsRng;
33
33
//! use secp256k1::{Secp256k1, Message};
34
- //! use secp256k1::hashes::sha256;
34
+ //! use secp256k1::hashes::{ sha256, Hash} ;
35
35
//!
36
36
//! let secp = Secp256k1::new();
37
37
//! let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);
38
- //! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
38
+ //! let digest = sha256::Hash::hash("Hello World!".as_bytes());
39
+ //! let message = Message::from_digest(digest.to_byte_array());
39
40
//!
40
41
//! let sig = secp.sign_ecdsa(&message, &secret_key);
41
42
//! assert!(secp.verify_ecdsa(&message, &sig, &public_key).is_ok());
47
48
//! ```rust
48
49
//! # #[cfg(all(feature = "global-context", feature = "hashes-std", feature = "rand-std"))] {
49
50
//! use secp256k1::{generate_keypair, Message};
50
- //! use secp256k1::hashes::sha256;
51
+ //! use secp256k1::hashes::{ sha256, Hash} ;
51
52
//!
52
53
//! let (secret_key, public_key) = generate_keypair(&mut rand::thread_rng());
53
- //! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
54
+ //! let digest = sha256::Hash::hash("Hello World!".as_bytes());
55
+ //! let message = Message::from_digest(digest.to_byte_array());
54
56
//!
55
57
//! let sig = secret_key.sign_ecdsa(message);
56
58
//! assert!(sig.verify(&message, &public_key).is_ok());
@@ -176,8 +178,6 @@ use core::{fmt, mem, str};
176
178
177
179
#[ cfg( all( feature = "global-context" , feature = "std" ) ) ]
178
180
pub use context:: global:: { self , SECP256K1 } ;
179
- #[ cfg( feature = "hashes" ) ]
180
- use hashes:: Hash ;
181
181
#[ cfg( feature = "rand" ) ]
182
182
pub use rand;
183
183
pub use secp256k1_sys as ffi;
@@ -198,34 +198,23 @@ pub use crate::scalar::Scalar;
198
198
/// Trait describing something that promises to be a 32-byte random number; in particular,
199
199
/// it has negligible probability of being zero or overflowing the group order. Such objects
200
200
/// may be converted to `Message`s without any error paths.
201
+ #[ deprecated(
202
+ since = "0.29.0" ,
203
+ note = "Please see v0.29.0 rust-secp256k1/CHANGELOG.md for suggestion"
204
+ ) ]
201
205
pub trait ThirtyTwoByteHash {
202
206
/// Converts the object into a 32-byte array
203
207
fn into_32 ( self ) -> [ u8 ; 32 ] ;
204
208
}
205
209
206
- #[ cfg( feature = "hashes" ) ]
207
- impl ThirtyTwoByteHash for hashes:: sha256:: Hash {
208
- fn into_32 ( self ) -> [ u8 ; 32 ] { self . to_byte_array ( ) }
209
- }
210
-
211
- #[ cfg( feature = "hashes" ) ]
212
- impl ThirtyTwoByteHash for hashes:: sha256d:: Hash {
213
- fn into_32 ( self ) -> [ u8 ; 32 ] { self . to_byte_array ( ) }
214
- }
215
-
216
- #[ cfg( feature = "hashes" ) ]
217
- impl < T : hashes:: sha256t:: Tag > ThirtyTwoByteHash for hashes:: sha256t:: Hash < T > {
218
- fn into_32 ( self ) -> [ u8 ; 32 ] { self . to_byte_array ( ) }
219
- }
220
-
221
210
/// A (hashed) message input to an ECDSA signature.
222
211
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
223
212
pub struct Message ( [ u8 ; constants:: MESSAGE_SIZE ] ) ;
224
213
impl_array_newtype ! ( Message , u8 , constants:: MESSAGE_SIZE ) ;
225
214
impl_pretty_debug ! ( Message ) ;
226
215
227
216
impl Message {
228
- /// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
217
+ /// Creates a [`Message`] from a 32 byte slice `digest`.
229
218
///
230
219
/// Converts a `MESSAGE_SIZE`-byte slice to a message object. **WARNING:** the slice has to be a
231
220
/// cryptographically secure hash of the actual message that's going to be signed. Otherwise
@@ -239,8 +228,6 @@ impl Message {
239
228
240
229
/// Creates a [`Message`] from a `digest`.
241
230
///
242
- /// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
243
- ///
244
231
/// The `digest` array has to be a cryptographically secure hash of the actual message that's
245
232
/// going to be signed. Otherwise the result of signing isn't a [secure signature].
246
233
///
@@ -250,8 +237,6 @@ impl Message {
250
237
251
238
/// Creates a [`Message`] from a 32 byte slice `digest`.
252
239
///
253
- /// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
254
- ///
255
240
/// The slice has to be 32 bytes long and be a cryptographically secure hash of the actual
256
241
/// message that's going to be signed. Otherwise the result of signing isn't a [secure
257
242
/// signature].
@@ -272,31 +257,9 @@ impl Message {
272
257
_ => Err ( Error :: InvalidMessage ) ,
273
258
}
274
259
}
275
-
276
- /// Constructs a [`Message`] by hashing `data` with hash algorithm `H`.
277
- ///
278
- /// Requires the feature `hashes` to be enabled.
279
- ///
280
- /// # Examples
281
- ///
282
- /// ```
283
- /// # #[cfg(feature = "hashes")] {
284
- /// use secp256k1::hashes::{sha256, Hash};
285
- /// use secp256k1::Message;
286
- ///
287
- /// let m1 = Message::from_hashed_data::<sha256::Hash>("Hello world!".as_bytes());
288
- /// // is equivalent to
289
- /// let m2 = Message::from(sha256::Hash::hash("Hello world!".as_bytes()));
290
- ///
291
- /// assert_eq!(m1, m2);
292
- /// # }
293
- /// ```
294
- #[ cfg( feature = "hashes" ) ]
295
- pub fn from_hashed_data < H : ThirtyTwoByteHash + hashes:: Hash > ( data : & [ u8 ] ) -> Self {
296
- <H as hashes:: Hash >:: hash ( data) . into ( )
297
- }
298
260
}
299
261
262
+ #[ allow( deprecated) ]
300
263
impl < T : ThirtyTwoByteHash > From < T > for Message {
301
264
/// Converts a 32-byte hash directly to a message without error paths.
302
265
fn from ( t : T ) -> Message { Message ( t. into_32 ( ) ) }
@@ -1043,24 +1006,6 @@ mod tests {
1043
1006
let sig = SECP256K1 . sign_ecdsa ( & msg, & sk) ;
1044
1007
assert ! ( SECP256K1 . verify_ecdsa( & msg, & sig, & pk) . is_ok( ) ) ;
1045
1008
}
1046
-
1047
- #[ cfg( feature = "hashes" ) ]
1048
- #[ test]
1049
- fn test_from_hash ( ) {
1050
- use hashes:: { sha256, sha256d, Hash } ;
1051
-
1052
- let test_bytes = "Hello world!" . as_bytes ( ) ;
1053
-
1054
- let hash = sha256:: Hash :: hash ( test_bytes) ;
1055
- let msg = Message :: from ( hash) ;
1056
- assert_eq ! ( msg. 0 , hash. to_byte_array( ) ) ;
1057
- assert_eq ! ( msg, Message :: from_hashed_data:: <hashes:: sha256:: Hash >( test_bytes) ) ;
1058
-
1059
- let hash = sha256d:: Hash :: hash ( test_bytes) ;
1060
- let msg = Message :: from ( hash) ;
1061
- assert_eq ! ( msg. 0 , hash. to_byte_array( ) ) ;
1062
- assert_eq ! ( msg, Message :: from_hashed_data:: <hashes:: sha256d:: Hash >( test_bytes) ) ;
1063
- }
1064
1009
}
1065
1010
1066
1011
#[ cfg( bench) ]
0 commit comments