我目前正在创建需要签名的加密文件格式.为此,我需要计算我写入流的内容的哈希码.
在.net框架中有许多可以使用的哈希算法,并且它工作正常,但它需要我处理流三次.
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);
}
Run Code Online (Sandbox Code Playgroud)
如果它被加密到文件,这是可以的,但如果它被加密到网络目的地,则字节不再可用.
所以基本上我只需要处理一次数据.在CodeProject上有一篇文章已经将CRC32实现为Stream,每次有写入数据时都会计算CRC32代码.
就像是:
byte[] content = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
using (FileStream …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个TCP客户端应用程序,为此,内置的“ net”包似乎可以完成这项工作。但是,我找不到该包的TypeScript定义。
https://nodejs.org/api/net.html
由于它内置在NodeJ中,因此几乎可以肯定它存在,但是它无法在任何地方找到有关它的任何信息。
谢谢!