16
16
17
17
use :: core:: fmt;
18
18
use :: { SecretKey , KeyPair , to_hex} ;
19
+ use ecdh:: SharedSecret ;
19
20
use constants:: SECRET_KEY_SIZE ;
20
21
21
22
macro_rules! impl_display_secret {
@@ -35,7 +36,7 @@ macro_rules! impl_display_secret {
35
36
36
37
hasher. write( DEBUG_HASH_TAG ) ;
37
38
hasher. write( DEBUG_HASH_TAG ) ;
38
- hasher. write( & self . serialize_secret ( ) ) ;
39
+ hasher. write( & self . secret_bytes ( ) ) ;
39
40
let hash = hasher. finish( ) ;
40
41
41
42
f. debug_tuple( stringify!( $thing) )
@@ -55,7 +56,7 @@ macro_rules! impl_display_secret {
55
56
let tag_hash = sha256:: Hash :: hash( tag. as_bytes( ) ) ;
56
57
engine. input( & tag_hash[ ..] ) ;
57
58
engine. input( & tag_hash[ ..] ) ;
58
- engine. input( & self . serialize_secret ( ) ) ;
59
+ engine. input( & self . secret_bytes ( ) ) ;
59
60
let hash = sha256:: Hash :: from_engine( engine) ;
60
61
61
62
f. debug_tuple( stringify!( $thing) )
@@ -67,7 +68,7 @@ macro_rules! impl_display_secret {
67
68
#[ cfg( all( not( feature = "std" ) , not( feature = "bitcoin_hashes" ) ) ) ]
68
69
impl :: core:: fmt:: Debug for $thing {
69
70
fn fmt( & self , f: & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
70
- write!( f, "<secret requires std feature to display>" )
71
+ write!( f, "<secret requires std or bitcoin_hashes feature to display>" )
71
72
}
72
73
}
73
74
}
@@ -91,7 +92,7 @@ pub struct DisplaySecret {
91
92
impl fmt:: Debug for DisplaySecret {
92
93
#[ inline]
93
94
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
94
- let mut slice = [ 0u8 ; 64 ] ;
95
+ let mut slice = [ 0u8 ; SECRET_KEY_SIZE * 2 ] ;
95
96
let hex = to_hex ( & self . secret , & mut slice) . expect ( "fixed-size hex serializer failed" ) ;
96
97
f. debug_tuple ( "DisplaySecret" )
97
98
. field ( & hex)
@@ -101,8 +102,8 @@ impl fmt::Debug for DisplaySecret {
101
102
102
103
impl fmt:: Display for DisplaySecret {
103
104
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
104
- for i in & self . secret {
105
- write ! ( f, "{:02x}" , i ) ?;
105
+ for byte in & self . secret {
106
+ write ! ( f, "{:02x}" , byte ) ?;
106
107
}
107
108
Ok ( ( ) )
108
109
}
@@ -113,33 +114,32 @@ impl SecretKey {
113
114
/// little-endian hexadecimal string using the provided formatter.
114
115
///
115
116
/// This is the only method that outputs the actual secret key value, and, thus,
116
- /// should be used with extreme precaution .
117
+ /// should be used with extreme caution .
117
118
///
118
- /// # Example
119
+ /// # Examples
119
120
///
120
121
/// ```
121
- /// # #[cfg(all(feature = "std", not(feature = "bitcoin_hashes")))] {
122
- /// use secp256k1::ONE_KEY;
123
- /// let key = ONE_KEY;
124
- /// // Normal display hides value
125
- /// assert_eq!(
126
- /// "SecretKey(#2518682f7819fb2d)",
127
- /// format!("{:?}", key)
128
- /// );
122
+ /// # #[cfg(feature = "std")] {
123
+ /// let key = secp256k1::ONE_KEY;
124
+ ///
125
+ /// // Normal debug hides value (`Display` is not implemented for `SecretKey`).
126
+ /// // E.g., `format!("{:?}", key)` prints "SecretKey(#2518682f7819fb2d)".
127
+ ///
129
128
/// // Here we explicitly display the secret value:
130
129
/// assert_eq!(
131
130
/// "0000000000000000000000000000000000000000000000000000000000000001",
132
131
/// format!("{}", key.display_secret())
133
132
/// );
133
+ /// // Also, we can explicitly display with `Debug`:
134
134
/// assert_eq!(
135
- /// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")" ,
136
- /// format!("{:?} ", key.display_secret())
135
+ /// format!("{:?}", key.display_secret()) ,
136
+ /// format!("DisplaySecret(\"{}\") ", key.display_secret())
137
137
/// );
138
138
/// # }
139
139
/// ```
140
140
#[ inline]
141
141
pub fn display_secret ( & self ) -> DisplaySecret {
142
- DisplaySecret { secret : self . serialize_secret ( ) }
142
+ DisplaySecret { secret : self . secret_bytes ( ) }
143
143
}
144
144
}
145
145
@@ -153,33 +153,66 @@ impl KeyPair {
153
153
/// # Example
154
154
///
155
155
/// ```
156
- /// # #[cfg(all( feature = "std", not(feature = "bitcoin_hashes")) )] {
156
+ /// # #[cfg(feature = "std")] {
157
157
/// use secp256k1::ONE_KEY;
158
158
/// use secp256k1::KeyPair;
159
159
/// use secp256k1::Secp256k1;
160
160
///
161
161
/// let secp = Secp256k1::new();
162
162
/// let key = ONE_KEY;
163
163
/// let key = KeyPair::from_secret_key(&secp, key);
164
- ///
165
- /// // Normal display hides value
166
- /// assert_eq!(
167
- /// "KeyPair(#2518682f7819fb2d)",
168
- /// format!("{:?}", key)
169
- /// );
170
164
/// // Here we explicitly display the secret value:
171
165
/// assert_eq!(
172
166
/// "0000000000000000000000000000000000000000000000000000000000000001",
173
167
/// format!("{}", key.display_secret())
174
168
/// );
169
+ /// // Also, we can explicitly display with `Debug`:
170
+ /// assert_eq!(
171
+ /// format!("{:?}", key.display_secret()),
172
+ /// format!("DisplaySecret(\"{}\")", key.display_secret())
173
+ /// );
174
+ /// # }
175
+ /// ```
176
+ #[ inline]
177
+ pub fn display_secret ( & self ) -> DisplaySecret {
178
+ DisplaySecret { secret : self . secret_bytes ( ) }
179
+ }
180
+ }
181
+
182
+ impl SharedSecret {
183
+ /// Formats the explicit byte value of the shared secret kept inside the type as a
184
+ /// little-endian hexadecimal string using the provided formatter.
185
+ ///
186
+ /// This is the only method that outputs the actual shared secret value, and, thus,
187
+ /// should be used with extreme caution.
188
+ ///
189
+ /// # Examples
190
+ ///
191
+ /// ```
192
+ /// # #[cfg(not(fuzzing))]
193
+ /// # #[cfg(feature = "std")] {
194
+ /// # use std::str::FromStr;
195
+ /// # use secp256k1::{SecretKey, PublicKey};
196
+ /// use secp256k1::ecdh::SharedSecret;
197
+ ///
198
+ /// # let pk = PublicKey::from_slice(&[3, 23, 183, 225, 206, 31, 159, 148, 195, 42, 67, 115, 146, 41, 248, 140, 11, 3, 51, 41, 111, 180, 110, 143, 114, 134, 88, 73, 198, 174, 52, 184, 78]).expect("hard coded slice should parse correctly");
199
+ /// # let sk = SecretKey::from_str("57f0148f94d13095cfda539d0da0d1541304b678d8b36e243980aab4e1b7cead").unwrap();
200
+ ///
201
+ /// let secret = SharedSecret::new(&pk, &sk);
202
+ /// // Here we explicitly display the secret value:
203
+ /// assert_eq!(
204
+ /// format!("{}", secret.display_secret()),
205
+ /// "cf05ae7da039ddce6d56dd57d3000c6dd91c6f1695eae47e05389f11e2467043"
206
+ /// );
207
+ /// // Also, we can explicitly display with `Debug`:
175
208
/// assert_eq!(
176
- /// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")" ,
177
- /// format!("{:?}", key .display_secret())
209
+ /// format!("{:?}", secret.display_secret()) ,
210
+ /// format!("DisplaySecret(\"{}\")", secret .display_secret())
178
211
/// );
179
212
/// # }
180
213
/// ```
181
214
#[ inline]
182
215
pub fn display_secret ( & self ) -> DisplaySecret {
183
- DisplaySecret { secret : self . serialize_secret ( ) }
216
+ DisplaySecret { secret : self . secret_bytes ( ) }
184
217
}
185
218
}
0 commit comments