Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Write trait #54

Open
ErQrYfkrju opened this issue Sep 24, 2020 · 2 comments
Open

Implement Write trait #54

ErQrYfkrju opened this issue Sep 24, 2020 · 2 comments
Labels

Comments

@ErQrYfkrju
Copy link

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
Copy link
Author

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants