Search code examples
c#xmlxml-parsingstreamchecksum

Stream chaining in computing a checksum: avoiding memory issues


I have a FileStream connected to a xml file that I would like to read directly into a SHA512 object in order to compute a hash for the purposes of a checksum (not a security use).

The issue is twofold:

  1. I want to omit some of the nodes in the xml,
  2. the file is quite large, and I would rather not load the whole thing into into memory

I can read the whole file into a xml structure, delete the node, then write it to a stream that would then be plugged into SHA512.ComputeHash, but that will cause a performance loss. I would prefer to be able to somehow do the deletion of the nodes as an operation on a stream and then chain the streams together somehow into a single stream that can be passed into SHA512.ComputeHash(Stream).

How can I accomplish this?


Solution

  • using (var hash = new SHA512Cng())
    using (var stream = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
    using (var writer = XmlWriter.Create(stream))
    using (var reader = XmlReader.Create("input.xml"))
    {
        while (reader.Read())
        {
            // ... write node to writer ...
        }
    
        writer.Flush();
        stream.FlushFinalBlock();
        var result = hash.Hash;
    }