我正在使用7zSDK压缩和解压缩文件。我想在压缩文件之前读取它,生成一个sha256散列,在文件上写入并压缩它。
解压缩时,我将读取哈希,将其存储在一个变量中,解压缩文件,并获得一个新的哈希来与存储在变量中的哈希进行比较,以检查文件的完整性。
在压缩文件时,我包括了这个块:
//Write the hash size from the original file
int HashCodeSize = Hash.generateSHA256Hash(input).Length;
byte[] hashSize = BitConverter.GetBytes(HashCodeSize);
output.Write(hashSize, 0, hashSize.Length);
//Write the hash from the original file
byte[] fileHashCode = new byte[8];
fileHashCode = Hash.generateSHA256Hash(input);
output.Write(fileHashCode, 0, fileHashCode.Length);在解压缩文件时,我会这样做:
//read the hash size from the original file
byte[] hashSize = new byte[4];
input.Read(hashSize, 0, 4);
int sizeOfHash = BitConverter.ToInt16(hashSize, 0);
//Read Hash
byte[] fileHash = new byte[sizeOfHash];
input.Read(fileHash, 0, 8);当我包含这两个块时,我从SDK中得到一个*未处理的异常,没有它们,程序运行得很好。
我就是这样生成哈希的:
public static byte[] generateSHA256Hash(Stream fileSource)
{
SHA256 fileHashed = SHA256Managed.Create();
return fileHashed.ComputeHash(fileSource);
}有人知道我做错了什么吗?

发布于 2018-08-07 13:10:30
在编写文件之前,将指针移动到文件的起始位置,解决了我的问题:
input.Seek(0, SeekOrigin.Begin);https://stackoverflow.com/questions/51672320
复制相似问题