Streaming hash
Axenow opened this issue · 1 comments
Axenow commented
Hi.
I try to calculate the hash of a file and it always different if I change the size of chunks.
What do I do wrong?
{
let mut count: usize = 0;
let mut hasher = xxh3::Hash128::with_seed(0);
let mut f = File::open("Cargo.lock").expect("no file found");
let mut buffer = vec![0; 256];
while let Ok(n) = f.read(&mut buffer[..]) {
hasher.write(&buffer);
count += n;
if n != 256 {
break;
}
}
println!("256: {:x}, {}", hasher.finish_ext(), count);
}
{
let mut count: usize = 0;
let mut hasher = xxh3::Hash128::with_seed(0);
let mut f = File::open("Cargo.lock").expect("no file found");
let mut buffer = vec![0; 240];
while let Ok(n) = f.read(&mut buffer[..]) {
hasher.write(&buffer);
count += n;
if n != 240 {
break;
}
}
println!("240: {:x}, {}", hasher.finish_ext(), count);
}
{
let mut count: usize = 0;
let mut hasher = xxh3::Hash128::with_seed(0);
let mut f = File::open("Cargo.lock").expect("no file found");
let mut buffer = vec![0; 200];
while let Ok(n) = f.read(&mut buffer[..]) {
hasher.write(&buffer);
count += n;
if n != 200 {
break;
}
}
println!("200: {:x}, {}", hasher.finish_ext(), count);
}
{
let mut f = File::open("Cargo.lock").expect("no file found");
let metadata = fs::metadata("Cargo.lock").expect("Unable to read metadata");
let mut hasher = xxh3::Hash128::with_seed(0);
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer[..]).expect("Can't read file");
hasher.write(&buffer);
println!("full: {:x}, {}", hasher.finish_ext(), buffer.len());
// println!("{} == {}", count, metadata.len());
}
The result is
256: 1ee448053144f22d2cbdbe45196fa5c6, 2824
240: 6ac3bc90beb022a8831158d0eb34a124, 2824
200: d700e496e7318a85318e88499afc3053, 2824
full: 7d86bdc8cdbe9934871303e33b3c2aeb, 2824
Axenow commented
Please, close the issue.
I'm a bit new in Rust and made the mistake in the code.
Sorry.