c# - 写入流时计算哈希

标签 c# .net stream cryptography hash

我目前正在创建一个需要签名的加密文件格式。 为此,我需要计算已写入流的内容的哈希码。

在.net framework中有很多hash算法可以使用,而且效果很好,但是需要我处理3次stream。

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);
}

如果它被加密到一个文件,这没问题,但如果它被加密到一个网络目的地,字节就不再可用了。

所以基本上我只需要处理一次数据。在 CodeProject 上有一个 article已将 CRC32 实现为 Stream,每次向其写入数据时都会计算 CRC32 代码。

类似于:

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);
}

显然 CRC32 不是散列算法,但如果有类似 HashStream 的东西采用 HashAlgorithm 就更好了。 HashStream 会在每次调用 write/read 时更新哈希值。

类似于:

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);
}

读取文件应该以类似的方式工作,所以当您读取文件时(不包括哈希),读取内容的哈希已经计算出来。

是否可以使用 .net 框架现有组件构建类似这样的东西?

编辑:

谢谢彼得!我不知道 CryptoStream 可以采用 HashAlgorithm。所以为了同时加密和散列,我可以做这样的事情:

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);
}

最佳答案

这是由 CryptoStream 为您完成的。

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

关于c# - 写入流时计算哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725507/

相关文章:

c# - Asp.NET MVC,自定义TextWriterTraceListener不创建文件

android - 更改音频流媒体站时,Android应用变慢

c# - 将尺寸较小的网格内的大矩形居中(并且 ClipToBounds 不起作用)

.net - 当 Directory.GetFiles() 发现文件名不是 "like"时抛出异常时如何处理异常?

.net - 如何判断字节数组是否已被压缩?

c# - 使用 LINQ 比较两个列表元素

apache-kafka - Kafka Streams拓扑的处理顺序是否指定?

dart - 是否可以将项目添加到 Dart 中流中包含的列表?

c# - HttpWebrequest 中 HTTP header 的严格排序

c# - 在 Linq 中使用函数