I have an inputStream
that I want to use to compute a hash and save the file to disk. I would like to know how to do that efficiently. Should I use some task to do that concurrently, should I duplicate the stream pass to two streams, one for the the saveFile
method and one for thecomputeHash
method, or should I do something else?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
What about using a hash algorithms that operate on a block level? You can add the block to the hash (using the TransformBlock) and subsequently write the block to the file foreach block in the stream.
Untested rough shot:
This method will copy and hash with chained streams.
Full sample see https://gist.github.com/dhcgn/da1637277d9456db9523a96a0a34da78
You'll need to stuff the stream's bytes into a
byte[]
in order to hash them.It might not be the best option, but I would opt to go for
Stream
descendant/wrapper, the one that would be pass-through for one actually writing the file to the disk.So:
Stream
Stream _inner;
that will be the target stream to writeWrite()
and all related stuffWrite()
hash the blocks of data and call_inner.Write()
Usage example
Here is my solution, it writes an array of structs (the ticks variable) as a csv file (using the CsvHelper nuget package) and then creates a hash for checksum purposes using the suffix .sha256
I do this by writing the csv to a memoryStream, then writing the memory stream to disk, then passing the memorystream to the hash algo.
This solution is keeping the entire file around as a memorystream. It's fine for everything except multi-gigabyte files that would run you out of ram. If I had to do this again, I'd probably try using CryptoStream approach, but this is good enough for my foreseeable purposes.
I have verified via a 3rd party tool that the hashes are valid.
Here is the code: