33
44use super :: * ;
55
6- use std:: convert:: TryInto ;
76use tox_binary_io:: * ;
87use tox_crypto:: * ;
98use crate :: dht:: * ;
109
1110use nom:: {
1211 flat_map,
13- map_opt,
1412 number:: complete:: le_u64,
1513 combinator:: { rest, rest_len} ,
1614 bytes:: complete:: take
1715} ;
16+ use sha2:: { Digest , Sha256 } ;
17+ use sha2:: digest:: generic_array:: typenum:: marker_traits:: Unsigned ;
18+
19+ /// The type of onion ping ID which is SHA256 hash.
20+ pub type PingId = [ u8 ; <Sha256 as Digest >:: OutputSize :: USIZE ] ;
1821
1922/** It's used for announcing ourselves to onion node and for looking for other
2023announced nodes.
@@ -172,7 +175,7 @@ Length | Content
172175#[ derive( Clone , Debug , Eq , PartialEq ) ]
173176pub struct OnionAnnounceRequestPayload {
174177 /// Onion ping id
175- pub ping_id : [ u8 ; 32 ] ,
178+ pub ping_id : PingId ,
176179 /// `PublicKey` we are searching for
177180 pub search_pk : PublicKey ,
178181 /// `PublicKey` that should be used for sending data packets
@@ -183,7 +186,7 @@ pub struct OnionAnnounceRequestPayload {
183186
184187impl FromBytes for OnionAnnounceRequestPayload {
185188 named ! ( from_bytes<OnionAnnounceRequestPayload >, do_parse!(
186- ping_id: map_opt! ( take! ( 32 ) , |bytes : & [ u8 ] | bytes . try_into ( ) . ok ( ) ) >>
189+ ping_id: call! ( PingId :: from_bytes ) >>
187190 search_pk: call!( PublicKey :: from_bytes) >>
188191 data_pk: call!( PublicKey :: from_bytes) >>
189192 sendback_data: le_u64 >>
@@ -239,7 +242,7 @@ mod tests {
239242 tox_crypto:: crypto_init( ) . unwrap( ) ,
240243 onion_announce_request_payload_encode_decode,
241244 OnionAnnounceRequestPayload {
242- ping_id: [ 42 ; 32 ] ,
245+ ping_id: [ 42 ; < Sha256 as Digest > :: OutputSize :: USIZE ] ,
243246 search_pk: gen_keypair( ) . 0 ,
244247 data_pk: gen_keypair( ) . 0 ,
245248 sendback_data: 12345
@@ -253,7 +256,7 @@ mod tests {
253256 let ( bob_pk, _bob_sk) = gen_keypair ( ) ;
254257 let shared_secret = encrypt_precompute ( & bob_pk, & alice_sk) ;
255258 let payload = OnionAnnounceRequestPayload {
256- ping_id : [ 42 ; 32 ] ,
259+ ping_id : [ 42 ; < Sha256 as Digest > :: OutputSize :: USIZE ] ,
257260 search_pk : gen_keypair ( ) . 0 ,
258261 data_pk : gen_keypair ( ) . 0 ,
259262 sendback_data : 12345
@@ -274,7 +277,7 @@ mod tests {
274277 let ( _eve_pk, eve_sk) = gen_keypair ( ) ;
275278 let shared_secret = encrypt_precompute ( & bob_pk, & alice_sk) ;
276279 let payload = OnionAnnounceRequestPayload {
277- ping_id : [ 42 ; 32 ] ,
280+ ping_id : [ 42 ; < Sha256 as Digest > :: OutputSize :: USIZE ] ,
278281 search_pk : gen_keypair ( ) . 0 ,
279282 data_pk : gen_keypair ( ) . 0 ,
280283 sendback_data : 12345
0 commit comments