Comments (5)
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.
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.
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.
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.
@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)
- ring affected by CVE-2022-37454? HOT 3
- Upgrade issues from sha-1 0.9.8 to sha1 0.10.x HOT 2
- [Feature Request] SHA-3 KMAC HOT 1
- Performance issue on SHA3-Keccak256 since v0.10.7 HOT 12
- comparing after dereferencing or unsafely indexing, which is faster? HOT 1
- Blake2 parameters cannot be used without a key HOT 3
- use keccak crate in k12 HOT 2
- streebog: Use const fn to generate `SHUFFLED_LIN_TABLE`
- fix(?): some `extern` functions couldn't be found HOT 2
- `sha2` fails to build in Windows on an ARM device HOT 2
- blake2: MAC variants should not be constructable with an empty key?
- sha2: Interested in a way to save/restore internal state HOT 1
- Strange quirk in SHA256 impl HOT 1
- "no such associated item" in IDE but build succeeds HOT 3
- jh: remove dependency on `ppv-lite86`
- Sha256::new() is always marked as an error in the vscode HOT 1
- can't build `[email protected]` HOT 1
- soft-sha512 code size seems unreasonably high on thumbv7em HOT 1
- Broken documentation: What on earth is "GenericArray" HOT 8
- sha256 has very wildly varying performance compared to ring between computers (same binary) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashes.