57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using System.Security.Cryptography;
|
|
|
|
namespace ln.tools.mailpdfextract;
|
|
|
|
public class HashingStream : Stream, IDisposable
|
|
{
|
|
public Stream BaseStream { get; }
|
|
|
|
SHA256 _sha256 = SHA256.Create();
|
|
|
|
public byte[] Hash
|
|
{
|
|
get
|
|
{
|
|
Dispose();
|
|
return _sha256.Hash;
|
|
}
|
|
}
|
|
|
|
public HashingStream(Stream baseStream)
|
|
{
|
|
BaseStream = baseStream;
|
|
}
|
|
|
|
public override void Flush() => BaseStream.Flush();
|
|
public override int Read(byte[] buffer, int offset, int count) => 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)
|
|
{
|
|
_sha256.TransformBlock(buffer, offset, count, null, 0);
|
|
BaseStream.Write(buffer, offset, count);
|
|
}
|
|
|
|
public override bool CanRead => false;
|
|
public override bool CanSeek => false;
|
|
public override bool CanWrite => true;
|
|
public override long Length => BaseStream.Length;
|
|
|
|
public override long Position
|
|
{
|
|
get => BaseStream.Position;
|
|
set => throw new NotSupportedException();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (BaseStream is Stream)
|
|
{
|
|
_sha256.TransformFinalBlock(null, 0, 0);
|
|
BaseStream?.Dispose();
|
|
base.Dispose();
|
|
}
|
|
}
|
|
|
|
} |