shepmaster / twox-hash

A Rust implementation of the XXHash algorithm.
MIT License
361 stars 41 forks source link

Implement Write trait #54

Open ErQrYfkrju opened 4 years ago

ErQrYfkrju commented 4 years ago

use hasher as writer to calculate hash

let mut data = vec![];
let f = File::open("/file");
let mut f = match f {
    Ok(file) => file,
    Err(e) => panic!("Error [{:}]",e),
};
let mut hasher = XxHash64::with_seed(0);
let f = TeeReader::new(&mut reader, &mut hasher);
let n = f.read_to_end(&mut data).expect("Didn't read enough");
println!("{:16x}", hasher.finish())
ErQrYfkrju commented 3 years ago

https://stackoverflow.com/questions/48533445/proper-way-to-hash-a-reader-in-rust I know there is a question here.. Could you, please, point the right way to get hash from this wrapper

shepmaster commented 3 years ago

Sorry, I missed this the first time around!

use std::fs::File;
use twox_hash::XxHash64;

fn main() {
    let mut f = File::open("/etc/hosts").expect("Unable to open file");

    let hasher = XxHash64::with_seed(0);
    let mut hw = HashWriter(hasher);

    io::copy(&mut f, &mut hw).expect("Unable to copy data");

    let hasher = hw.0;
    println!("{}", hasher.finish());
}

See also https://github.com/shepmaster/twox-hash/blob/master/src/bin/hash_file.rs

I've updated the SO answer as well.