计算部分流的MD5哈希值

时间:2011-05-18 07:12:29

标签: .net stream iostream

我有一个以自定义文件格式存储的大型数据集(~1GB),其最后16个字节是文件所有先前字节的MD5哈希值。

我想使用HashAlgorithm.ComputeHash(Stream)针对嵌入式MD5验证此文件的MD5,但是这将计算整个文件的哈希值,包括最后16字节中的哈希值,这显然不会起作用。

如何计算流的PART的MD5哈希值?我知道我可以将流读入数组并将其传递给HashAlgorithm.ComputeHash(字节),但是在内存中复制这1GB数据的开销是令人望而却步的。

3 个答案:

答案 0 :(得分:5)

取自here,你也可以通过其他方式获得这样做。

制作一个部分文件流类,读取你想要的大小并制作它的哈希值。

 class PartialFileStream : FileStream
{
    public PartialFileStream(string path, FileMode mode, long startPosition, long endPosition): base(path, mode)
{
  base.Seek(startPosition, SeekOrigin.Begin);
  ReadTillPosition = endPosition;
}

public long ReadTillPosition { get; set; }

public override int Read(byte[] array, int offset, int count)
{
 if (base.Position >= this.ReadTillPosition)
   return 0;

 if (base.Position + count > this.ReadTillPosition)
   count = (int)(this.ReadTillPosition - base.Position);

 return base.Read(array, offset, count);
  }
}

答案 1 :(得分:0)

您可以使用FileStream.Seek选项搜索流的特定位置并从那里读取。

答案 2 :(得分:0)

我发现自己在6个月内第二次需要此服务,因此发布了针对部分输入流的解决方案。

class PartialStream: Stream {
    public Stream Source { get; }
    public long Offset { get; }
    public override long Length { get; }

    private long End => Offset + Length;

    public override bool CanRead => true;

    public override bool CanSeek => false;

    public override bool CanWrite => false;

    public override long Position {
        get => Source.Position - Offset;
        set => throw new NotSupportedException();
    }

    public PartialStream(Stream source, long length) {
        Offset = source.Position;
        Length = length;
    }

    public PartialStream(Stream source, long offset, long length, bool seekToOffset = true) {
        if (seekToOffset) source.Seek(offset, SeekOrigin.Begin);
        Offset = offset;
        Length = length;
    }

    public override int Read(byte[] array, int offset, int count) {
        if (Source.Position >= End) return 0;

        if (Source.Position + count > End)
            count = (int)(End - Source.Position);

        return Source.Read(array, offset, count);
    }

    public override void Flush() => throw new NotSupportedException();
    public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
    public override void SetLength(long value) => throw new NotSupportedException();
    public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}