From ba5882c2b9835d9662986ec63e39912d7a2e60c2 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Tue, 18 Jan 2022 20:10:23 +0100 Subject: [PATCH] revert commit 17cd68f --- src/packet/container.rs | 41 +++++++++++++++++++++++++++-------------- src/parse.rs | 4 +--- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/packet/container.rs b/src/packet/container.rs index dfbb624..3a4bb26 100644 --- a/src/packet/container.rs +++ b/src/packet/container.rs @@ -8,11 +8,11 @@ use std::hash::{Hash, Hasher}; use std::slice; use std::vec; -use xxhash_rust::xxh3::Xxh3; - use crate::{ Packet, + crypto::hash, packet::Iter, + types::HashAlgorithm, }; /// A packet's body holds either unprocessed bytes, processed bytes, @@ -126,7 +126,7 @@ pub struct Container { body: Body, /// We compute a digest over the body to implement comparison. - body_digest: u64, + body_digest: Vec, } assert_send_and_sync!(Container); @@ -138,6 +138,14 @@ impl std::ops::Deref for Container { } } +// Pick the fastest hash function from the SHA2 family for the +// architectures word size. On 64-bit architectures, SHA512 is almost +// twice as fast, but on 32-bit ones, SHA256 is faster. +#[cfg(target_pointer_width = "64")] +const CONTAINER_BODY_HASH: HashAlgorithm = HashAlgorithm::SHA512; +#[cfg(not(target_pointer_width = "64"))] +const CONTAINER_BODY_HASH: HashAlgorithm = HashAlgorithm::SHA256; + impl PartialEq for Container { fn eq(&self, other: &Container) -> bool { use Body::*; @@ -169,7 +177,7 @@ impl Default for Container { fn default() -> Self { Self { body: Body::Structured(Vec::with_capacity(0)), - body_digest: 0, + body_digest: Vec::with_capacity(0), } } } @@ -178,7 +186,7 @@ impl From> for Container { fn from(packets: Vec) -> Self { Self { body: Body::Structured(packets), - body_digest: 0, + body_digest: Vec::with_capacity(0), } } } @@ -296,31 +304,36 @@ impl Container { } /// Returns the hash for the empty body. - fn empty_body_digest() -> u64 { + fn empty_body_digest() -> Vec { lazy_static::lazy_static!{ - static ref DIGEST: u64 = { - Container::make_body_hash().digest() + static ref DIGEST: Vec = { + let mut h = Container::make_body_hash(); + let mut d = vec![0; h.digest_size()]; + let _ = h.digest(&mut d); + d }; } - *DIGEST + DIGEST.clone() } /// Creates a hash context for hashing the body. pub(crate) // For parse.rs - fn make_body_hash() -> Box { - Box::new(Xxh3::new()) + fn make_body_hash() -> Box { + CONTAINER_BODY_HASH.context() + .expect("CONTAINER_BODY_HASH must be implemented") } /// Hashes content that has been streamed. pub(crate) // For parse.rs - fn set_body_hash(&mut self, h: Box) { - self.body_digest = h.digest(); + fn set_body_hash(&mut self, mut h: Box) { + self.body_digest.resize(h.digest_size(), 0); + let _ = h.digest(&mut self.body_digest); } pub(crate) fn body_digest(&self) -> String { - format!("{:08X}", self.body_digest) + crate::fmt::hex::encode(&self.body_digest) } // Converts an indentation level to whitespace. diff --git a/src/parse.rs b/src/parse.rs index 1fe8899..23084af 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -183,8 +183,6 @@ use std::fmt; use std::path::Path; use std::result::Result as StdResult; -use xxhash_rust::xxh3::Xxh3; - use ::buffered_reader::*; use crate::{ @@ -3306,7 +3304,7 @@ pub struct PacketParser<'a> { /// We compute a hashsum over the body to implement comparison on /// containers that have been streamed. - body_hash: Option>, + body_hash: Option>, state: PacketParserState, } -- 2.34.1