mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-27 16:51:34 +00:00
Apparently this is truly the fastest way
This commit is contained in:
@ -21,7 +21,6 @@
|
|||||||
//! Individual messages encoding.
|
//! Individual messages encoding.
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use bytes::buf::BufMut;
|
|
||||||
use crypto::symmetriccipher::SynchronousStreamCipher;
|
use crypto::symmetriccipher::SynchronousStreamCipher;
|
||||||
use futures::Poll;
|
use futures::Poll;
|
||||||
use futures::StartSend;
|
use futures::StartSend;
|
||||||
@ -65,16 +64,17 @@ impl<S> Sink for EncoderMiddleware<S>
|
|||||||
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||||
let capacity = item.len() + self.hmac_key.digest_algorithm().output_len;
|
let capacity = item.len() + self.hmac_key.digest_algorithm().output_len;
|
||||||
|
|
||||||
let mut out_buffer = BytesMut::with_capacity(capacity);
|
// Apparently this is the fastest way of doing.
|
||||||
// Note: Alternatively to `extend`, we could also call `advance_mut()`, which will add
|
// See https://gist.github.com/kirushik/e0d93759b0cd102f814408595c20a9d0
|
||||||
// uninitialized bytes to the buffer. But that's unsafe.
|
let mut out_buffer = BytesMut::from(vec![0; capacity]);
|
||||||
out_buffer.extend_from_slice(&vec![0; item.len()]);
|
|
||||||
self.cipher_state.process(&item, &mut out_buffer);
|
|
||||||
|
|
||||||
let signature = hmac::sign(&self.hmac_key, &out_buffer);
|
{
|
||||||
out_buffer.put_slice(signature.as_ref());
|
let (out_data, out_sign) = out_buffer.split_at_mut(item.len());
|
||||||
|
self.cipher_state.process(&item, out_data);
|
||||||
|
|
||||||
debug_assert_eq!(out_buffer.len(), capacity);
|
let signature = hmac::sign(&self.hmac_key, out_data);
|
||||||
|
out_sign.copy_from_slice(signature.as_ref());
|
||||||
|
}
|
||||||
|
|
||||||
self.raw_sink.start_send(out_buffer)
|
self.raw_sink.start_send(out_buffer)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user