Calculate hash when writing to stream

孤人 提交于 2020-01-31 04:31:25

问题


I am currently creating an encrypted file format that needs to be signed. For this I need to calculate the hash code of the content I have written to a stream.

In the .net framework there is numerous hash algorithms that can be used, and it works fine, but it requires me to process the stream three times.

byte[] content = new byte[] { 0, 1, 2, 3, 4, 5, 6 };

using (Stream fileStream = File.Open("myfile.bin", FileMode.Create))
{
    //Write content
    fileStream.Write(content, 0, content.Length);
}

byte[] hashValue = null;
using (Stream fileStream = File.Open("myfile.bin", FileMode.Open))
{
    HashAlgorithm hash = SHA256.Create();
    hashValue = hash.ComputeHash(fileStream);
}

using (Stream fileStream = File.Open("myfile.bin", FileMode.Append))
{
    fileStream.Write(hashValue, 0, hashValue.Length);
}

This is okay if it is encrypted to a file, but if it is encrypted to a network destination, the bytes are no longer available.

So basically I need to only process the data once. On CodeProject there is an article that has implemented CRC32 as a Stream that calculates the CRC32 code every time there is written data to it.

Something like:

byte[] content = new byte[] { 0, 1, 2, 3, 4, 5, 6 };

using (FileStream fileStream = File.Create("myfile.bin"))
using (Stream crcStream = new CRCStream(fileStream)) //Takes a base stream
{
    //Write content
    crcStream.Write(content, 0, content.Length); 

    //Write checksum
    fileStream.Write(crcStream.WriteCRC, 0, 4);
}

Obviously CRC32 is not a hash alogorithm, but it would be nice to have something like a HashStream that takes a HashAlgorithm. The HashStream would update the hash value every time write/read is called.

Something like:

byte[] content = new byte[] { 0, 1, 2, 3, 4, 5, 6 };

HashAlgorithm hashAlgo = SHA256.Create();

using (FileStream fileStream = File.Create("myfile.bin"))
using (HashStream hashStream = new HashStream(hashAlgo, fileStream))
{
    //Write content to HashStream 
    hashStream.Write(content, 0, content.Length);

    //Write checksum
    fileStream.Write(hashStream.HashValue, 0, hashAlgo.HashSize / 8);
}

Reading the files should work in a similar way, so when you have read the file (not including the hash), the hash of the read content has already been calculated.

Is it possible to construct something like this using the .net frameworks existing components?

Edit:

Thanks Peter! I did not know CryptoStream could take a HashAlgorithm. So for encrypting and hashing at the same time, I could do something like this:

byte[] content = new byte[] { 0, 1, 2, 3, 4, 5, 6 };

SymmetricAlgorithm cryptoSymetric = Aes.Create();
HashAlgorithm cryptoHash = SHA256.Create();

using (FileStream file = new FileStream("Crypto.bin", FileMode.Create, FileAccess.Write))
using (CryptoStream hashStream = new CryptoStream(file, cryptoHash, CryptoStreamMode.Write))
using (CryptoStream cryptStream = new CryptoStream(hashStream, cryptoSymetric.CreateEncryptor(), CryptoStreamMode.Write))
{
    cryptStream.Write(content, 0, content.Length);
    cryptStream.FlushFinalBlock();

    byte[] hashValue = cryptoHash.Hash;

    file.Write(hashValue, 0, hashValue.Length);
}

回答1:


It's done for you by CryptoStream.

SHA256 hashAlg = new SHA256Managed();
CryptoStream cs = new CryptoStream(_out, hashAlg, CryptoStreamMode.Write);
// Write data here
cs.FlushFinalBlock();
byte[] hash = hashAlg.Hash;



回答2:


You can do block-by-block hash transforms using HashAlgorithm.TransformBlock. This can be wrapped in a Stream implementation, so you could have a HashStream as you are suggesting. Remember to call TransformFinalBlock before getting the hash value.

HashAlgorithm is abstract, so of course you need to pick the concrete implementation you want. There are MD5, the SHA family and others to pick from.



来源:https://stackoverflow.com/questions/4725507/calculate-hash-when-writing-to-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!