0

Why in this soulution which I found there is no Loop to use buffor few times?

using System.IO;
using System.Security.Cryptography;

private static string GetChecksum(string file)
{
    using (FileStream stream = File.OpenRead(file))
    {
        SHA256Managed sha = new SHA256Managed();
        byte[] checksum = sha.ComputeHash(stream);
        return BitConverter.ToString(checksum).Replace("-", String.Empty);
    }
}

I'm trying to generate SHA checksum for +2GB file. How it should be?

Tomasz M
  • 53
  • 8
  • Possible duplicate of [Get a file SHA256 Hash code and Checksum](https://stackoverflow.com/questions/38474362/get-a-file-sha256-hash-code-and-checksum) – Sinatr Nov 13 '17 at 14:20
  • @Sinatr Example code here, is from there :) – bot_insane Nov 13 '17 at 14:21
  • @AramKocharyan, the questions (as it seems to me) are too. I hope OP will clarify if I am wrong. – Sinatr Nov 13 '17 at 14:26
  • 1
    ComputeHash() slurps the entire stream by itself. Convenient. That buffer is [still there](https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/hashalgorithm.cs,57). – Hans Passant Nov 13 '17 at 16:24

2 Answers2

0

You are using HashAlgorithm.ComputeHash(Stream) override.

The loop is under the hood.

Here is simplified source code of ComputeHash method:

public byte[] ComputeHash(Stream inputStream) 
{
    ...
    // Default the buffer size to 4K.
    byte[] buffer = new byte[4096]; 
    int bytesRead;
    do { 
        bytesRead = inputStream.Read(buffer, 0, 4096); 
        if (bytesRead > 0) {
            HashCore(buffer, 0, bytesRead);  // Actual hashing
        }
    } while (bytesRead > 0);
    ...
}

As you can see ComputeHash is actually hashing by 4096 byte chunks.

bot_insane
  • 2,545
  • 18
  • 40
0

Why in this soulution which I found there is no Loop to use buffor few times?

Because ComputeHash takes care of reading the entire stream and computing the hash value.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324