写入流时计算哈希时计、算哈希

2023-09-03 16:51:32 作者:First‖初遇

我目前正在创建一个需要签署一个加密的文件格式。 为此,我需要计算自己编写的流内容的哈希值code。

在.NET Framework的存在,可用于众多的散列算法,并能正常工作,但它需要我来处理数据流的三倍。

  byte []的内容=新的字节[] {0,1,2,3,4,5,6};

使用(流FILESTREAM = File.open方法(myfile.bin,FileMode.Create))
{
    //写的内容
    fileStream.Write(含量,0,content.Length);
}

byte []的散列值= NULL;
使用(流FILESTREAM = File.open方法(myfile.bin,FileMode.Open))
{
    HashAlgorithm散列= SHA256.Create();
    散列值= hash.ComputeHash(FILESTREAM);
}

使用(流FILESTREAM = File.open方法(myfile.bin,FileMode.Append))
{
    fileStream.Write(散列值,0,hashValue.Length);
}
 

这是好的,如果它是加密一个文件,但如果它被加密到网络目标,字节不再可用。

所以基本上我只需要处理一次数据。在$ C $的CProject也已经实现CRC32作为文章一个流,每一次计算CRC32 code中写入数据到它。

是这样的:

  byte []的内容=新的字节[] {0,1,2,3,4,5,6};

使用(的FileStream FILESTREAM = File.Create(myfile.bin))
使用(流crcStream =新CRCStream(FILESTREAM))//获取一个基本流
{
    //写的内容
    crcStream.Write(含量,0,content.Length);

    //写入校验
    fileStream.Write(crcStream.WriteCRC,0,4);
}

 
T5大牛带你解析 如何实现分布式技术

显然CRC32不是一个哈希算法FFT,但它会是不错的东西就像一个HashStream,需要一个HashAlgorithm。该HashStream将更新哈希值,每次读/写时调用。

是这样的:

  byte []的内容=新的字节[] {0,1,2,3,4,5,6};

HashAlgorithm hashAlgo = SHA256.Create();

使用(的FileStream FILESTREAM = File.Create(myfile.bin))
使用(HashStream hashStream =新HashStream(hashAlgo,文件流))
{
    //内容写入HashStream
    hashStream.Write(含量,0,content.Length);

    //写入校验
    fileStream.Write(hashStream.HashValue,0,hashAlgo.HashSize / 8);
}  

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

是否有可能建立这样的使用现有组件的.NET框架?

编辑:

感谢彼得!我不知道的CryptoStream可以采取HashAlgorithm。因此,对于加密和散列在同一时间,我可以做这样的事情:

  byte []的内容=新的字节[] {0,1,2,3,4,5,6};

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

使用(的FileStream文件=新的FileStream(Crypto.bin,FileMode.Create,FileAccess.Write))
使用(CryptoStream的hashStream =新CryptoStream的(文件,cryptoHash,CryptoStreamMode.Write))
使用(CryptoStream的cryptStream =新的CryptoStream(hashStream,cryptoSymetric.CreateEncryptor(),CryptoStreamMode.Write))
{
    cryptStream.Write(含量,0,content.Length);
    cryptStream.FlushFinalBlock();

    byte []的散列值= cryptoHash.Hash;

    file.Write(散列值,0,hashValue.Length);
}  

解决方案

它是由CryptoStream的为你做。

 SHA256 hashAlg =新SHA256Managed();
CryptoStream的CS =新的CryptoStream(_out,hashAlg,CryptoStreamMode.Write);
//此处写数据
cs.FlushFinalBlock();
byte []的哈希值= hashAlg.Hash; 

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

解决方案

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;

相关推荐