38
38
//!
39
39
//! ```rust
40
40
//! extern crate secp256k1;
41
+ //! # #[cfg(feature="bitcoin_hashes")]
42
+ //! extern crate bitcoin_hashes;
41
43
//! # #[cfg(feature="rand")]
42
44
//! extern crate rand;
43
45
//!
44
46
//! #
45
47
//! # fn main() {
46
- //! # #[cfg(feature="rand")] {
47
- //! use rand::OsRng;
48
+ //! # #[cfg(all( feature="rand", feature="bitcoin_hashes") )] {
49
+ //! use rand::rngs:: OsRng;
48
50
//! use secp256k1::{Secp256k1, Message};
51
+ //! use bitcoin_hashes::sha256;
49
52
//!
50
53
//! let secp = Secp256k1::new();
51
54
//! let mut rng = OsRng::new().expect("OsRng");
52
55
//! let (secret_key, public_key) = secp.generate_keypair(&mut rng);
53
- //! let message = Message::from_slice(&[0xab; 32]).expect("32 bytes" );
56
+ //! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes() );
54
57
//!
55
58
//! let sig = secp.sign(&message, &secret_key);
56
59
//! assert!(secp.verify(&message, &sig, &public_key).is_ok());
57
60
//! # } }
58
61
//! ```
59
62
//!
60
- //! The above code requires `rust-secp256k1` to be compiled with the `rand`
63
+ //! The above code requires `rust-secp256k1` to be compiled with the `rand` and `bitcoin_hashes`
61
64
//! feature enabled, to get access to [`generate_keypair`](struct.Secp256k1.html#method.generate_keypair)
62
- //! Alternately, keys can be parsed from slices, like
65
+ //! Alternately, keys and messages can be parsed from slices, like
63
66
//!
64
67
//! ```rust
65
68
//! # fn main() {
68
71
//! let secp = Secp256k1::new();
69
72
//! let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
70
73
//! let public_key = PublicKey::from_secret_key(&secp, &secret_key);
74
+ //! // This is unsafe unless the supplied byte slice is the output of a cryptographic hash function.
75
+ //! // See the above example for how to use this library together with bitcoin_hashes.
71
76
//! let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
72
77
//!
73
78
//! let sig = secp.sign(&message, &secret_key);
@@ -476,7 +481,12 @@ impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE);
476
481
impl_pretty_debug ! ( Message ) ;
477
482
478
483
impl Message {
479
- /// Converts a `MESSAGE_SIZE`-byte slice to a message object
484
+ /// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
485
+ ///
486
+ /// Converts a `MESSAGE_SIZE`-byte slice to a message object. **WARNING:** the slice has to be a
487
+ /// cryptographically secure hash of the actual message that's going to be signed. Otherwise
488
+ /// the result of signing isn't a
489
+ /// [secure signature](https://twitter.com/pwuille/status/1063582706288586752).
480
490
#[ inline]
481
491
pub fn from_slice ( data : & [ u8 ] ) -> Result < Message , Error > {
482
492
if data == [ 0 ; constants:: MESSAGE_SIZE ] {
@@ -493,7 +503,8 @@ impl Message {
493
503
}
494
504
}
495
505
496
- /// Constructs a `Message` by hashing `data` with hash algorithm `H`.
506
+ /// Constructs a `Message` by hashing `data` with hash algorithm `H`. This requires the feature
507
+ /// `bitcoin_hashes` to be enabled.
497
508
/// ```rust
498
509
/// extern crate bitcoin_hashes;
499
510
/// use secp256k1::Message;
0 commit comments