0

in C# I am using Filestream to write to a new file. I am then hashing this file to get SHA256 hash to verify later. I would like to remove this extra file read, and simply hash file as it is being streamed.

            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            { ...stream file, hash while doing so, give hash upon write finish... }

I have looked at examples of CryptoStream and others, but it is not clear how have a normal filestream that writes to a new file and get a hash while reading. Is this possible, and if so how?

Jaren L
  • 1
  • 1
  • Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see what questions you can ask. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Dec 16 '20 at 18:56
  • The only way to hash something is by having _all_ the contents. In this case, the full `byte[]` of the file contents. When you write the file, you already know the file contents. – gunr2171 Dec 16 '20 at 18:56
  • @gunr2171 So would that mean there will always be 2 reads? 1. Read + write to new file 2. Read + Hash ? – Jaren L Dec 16 '20 at 19:03
  • No. You do one write to make the file. You don't need to read again because you already know what you wrote. You can hash any byte array, it doesn't have to come from a file. You could hash an arbitrary string, for example. – gunr2171 Dec 16 '20 at 19:05

0 Answers0