Giter Club home page Giter Club logo

Comments (5)

newpavlov avatar newpavlov commented on June 8, 2024 1

You are right that such method is nice to have. Initially I thought about implementing Write trait, but I was hesitant to add it, as it would be incompatible with no_std and I didn't want to add std feature to every digest crate. But thanks to your issue it seems I found a good way to do it.

I've added DigestReader trait to the digest crate behind std feature gate. In contrast to your proposal it's an additive feature, so while digest crates do not use std feature your can enable it in Cargo.toml:

[dependencies]
blake2 = "0.5.1"
digest = {version="0.5.1", features=["std"]}

And then use DigestReader in your code:

let mut hash = Blake2b::default();
hash.read_from(&mut file).unwrap();
let result = hash.result();
println!("{:x}\t{}", result, path);

As afterthought I am thinking it's also worth to add a convenience function which will look like this:

pub fn digest_reader<D>(source: &mut io::Read)
    -> io::Result<GenericArray<u8, D::OutputSize>>
    where D: Default + DigestReader + FixedOutput
{
    let mut digest: D = Default::default();
    digest.read_from(source)?;
    Ok(digest.fixed_result())
}

UPD: I've added it in the digest v0.5.2.

from hashes.

Wallacoloo avatar Wallacoloo commented on June 8, 2024

I guess this method would actually go under the Input trait in the digest crate - not Digest. In any case, I see the Crates.io links are outdated & digest is hosted at https://github.com/RustCrypto/traits now. Should I move the issue there?

from hashes.

Wallacoloo avatar Wallacoloo commented on June 8, 2024

Ok, I'm using the digest_reader function in my own crate now. I don't have any tests for this portion yet, but it's compiling alright!

from hashes.

newpavlov avatar newpavlov commented on June 8, 2024

BTW you can write your code shorter without unnecessary copy:

&Some(hash) => {
    let mut file = File::open(path).expect("Failed to open file");
    let result = digest_reader::<Sha256>(&mut file).expect("Failed to read data from file");
    hash == result.as_slice()
}

Also in the next version of digest I will add digest_reader to the trait, so we could use Sha256::digest_reader(..) without turbofish operator. Should've done it initially, but forgot about it for some reason...

from hashes.

Wallacoloo avatar Wallacoloo commented on June 8, 2024

@newpavlov Thanks for the tip. FWIW, this portion of the code is tested now, so the DigestReader trait seems to be working as expected.

from hashes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.