Getting multiple hash digests while hashing a file iteratively

Assumptions for removing corner cases from example: files are at least 2 mb in size.

I want to create two hashes from the same file.
The first hash should be created from the first mb of the file, the second one should be created from the entire file (including the first mb).

If I use the implementations from Rust Crypto, I have to calculate the hash for the first mb twice, as the only way to access the hash is to call finalize and thereby consuming the hasher, or keeping the hasher but also resetting it in the process (if I got that correctly). There also seems to be no way to transfer the state of one e.g. Sha256 struct to another one (without redoing the hash calculation), so I don't see a way to avoid recalculating the hash for the first part of the file.

Am I getting this right or did I miss something? And if I am right, is there a good reason for why this is not possible?

You should be able to clone the hasher, so you can finalize one and keep using the other.

2 Likes

I've tried that approach in yadf but didn't see any meaningful improvement. Certainly not any improvement worth the increase in code complexity.

1 Like

Thank you, that's exactly what I was looking for; the .clone() method just didn't show up in the suggestions in my code editor

Ah nice, yeah, that's the usecase I also have in mind:D I'll check nevertheless becauce I have a lot of big audio files laying around and it might save me a few Gb of reading on each run

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.