Add system hasher

This commit is contained in:
Igor Katson 2021-07-01 17:18:07 +01:00
parent 4b6ed36927
commit a6981231c1
5 changed files with 67 additions and 4 deletions

View file

@ -121,7 +121,7 @@ impl<'a> FileOps<'a> {
let mut read_buffer = vec![0u8; 65536];
for piece_info in self.lengths.iter_piece_infos() {
let mut computed_hash = sha1w::Sha1Openssl::new();
let mut computed_hash = sha1w::Sha1System::new();
let mut piece_remaining = piece_info.len as usize;
let mut some_files_broken = false;
let mut at_least_one_file_required = current_file.full_file_required;
@ -221,7 +221,7 @@ impl<'a> FileOps<'a> {
piece_index: ValidPieceIndex,
last_received_chunk: &ChunkInfo,
) -> anyhow::Result<bool> {
let mut h = sha1w::Sha1Openssl::new();
let mut h = sha1w::Sha1System::new();
let piece_length = self.lengths.piece_length(piece_index);
let mut absolute_offset = self.lengths.piece_offset(piece_index);
let mut buf = vec![0u8; std::cmp::min(65536, piece_length as usize)];

View file

@ -266,7 +266,6 @@ where
PREAMBLE_LEN
}
Message::Piece(p) => {
// below code is wrong, need to serialize len_prefix
let block_len = p.block.as_ref().len();
let payload_len = 8 + block_len;
let msg_len = PREAMBLE_LEN + payload_len;

View file

@ -1,8 +1,10 @@
use std::io::Write;
// Wrapper for sha1 libraries.
// Sha1 computation is the majority of CPU usage of this library.
// openssl seems 2-3x faster, so using it for now, but
// leaving the pure-rust impl here too. Maybe someday make them
// runtime swappable.
// runtime swappable or enabled with a feature.
pub trait ISha1 {
fn new() -> Self;
@ -47,3 +49,27 @@ impl ISha1 for Sha1Openssl {
self.inner.finish()
}
}
pub struct Sha1System {
inner: crypto_hash::Hasher,
}
impl ISha1 for Sha1System {
fn new() -> Self {
Self {
inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1),
}
}
fn update(&mut self, buf: &[u8]) {
self.inner.write_all(buf).unwrap();
}
fn finish(mut self) -> [u8; 20] {
let result = self.inner.finish();
assert_eq!(result.len(), 20);
let mut result_arr = [0u8; 20];
result_arr.copy_from_slice(&result);
result_arr
}
}