我需要读取一个未知长度的流,不包括最后20个字节(哈希数据)。设置大致为:
源流(SHA1哈希最后20字节)->SHA1哈希流(动态计算,并在流结束时与嵌入流哈希进行比较)->AES解密流->处理数据。。。
在处理之前,我无法缓冲整个源流,因为它可能有很多GB,这一切都需要动态进行。源流不可查找。目前,SHA1流正在将最后20个字节读取到缓冲区中,这会破坏所有内容,我不知道有什么方法可以控制这种行为。
我想在Source和SHA1流之间插入一个包装流,实现一个滚动缓冲区(?),将源流以4096字节的块形式呈现给AES包装器,然后在最后一次读取时"伪造"流的末尾20字节。然后,20字节的散列将通过一个属性公开。
这会是最好的解决方案吗?我将如何实施它?
粗略的代码流如下(来自内存,可能不会编译):
SourceStream = TcpClient.Stream
HashedStream = New CryptoStream(SourceStream, Sha1Hasher, CryptoStreamMode.Read)
AesDecryptedStream = New CryptoStream(HashedStream, AesDecryptor, CryptoStreamMode.Read)
' Read out and deserialize data
AesDecryptedStream.Read(etc...)
' Check if signatures match, throw data away if not
If Not Sha1Hash.SequenceEqual(ExpectedHash)
' Do stuff with the data here
编辑:流格式如下:
[ StreamFormat | String | Required ]
[ WrapperFlags | 8 Bit BitArray | Required ]
[ Sha1 Hashed Data Wrapper | Optional ]
[ AesIV | 16 Bytes | Required if Aes Encrypted ]
[ Aes Encrypted Data Wrapper | Optional ]
[ Gzip Compressed Data Wrapper | Optional ]
[ Payload Data | Binary | Required ]
[ End Gzip Compressed Data ]
[ End Aes Encrypted Data ]
[ End Sha1 Hashed Data ]
[ Sha1HashValue | 20 Bytes | Required if Sha1 Hashed ]
我已经为您编写了一个快速的小流,它提前缓冲20个字节。我正确覆盖的唯一实际实现是Read()
成员,您可能需要根据您的情况适当地检查其他Stream
成员。还有免费的测试课程!奖金我对它进行了更彻底的测试,但您可以根据自己的意愿调整这些测试用例。哦,顺便说一下,我没有测试长度小于20字节的流。
测试用例
[TestClass]
public class TruncateStreamTests
{
[TestMethod]
public void TestTruncateLast20Bytes()
{
string testInput = "This is a string.-- final 20 bytes --";
string expectedOutput = "This is a string.";
string testOutput;
using (var testStream = new StreamWhichEndsBeforeFinal20Bytes(new MemoryStream(Encoding.ASCII.GetBytes(testInput))))
using (var streamReader = new StreamReader(testStream, Encoding.ASCII))
{
testOutput = streamReader.ReadLine();
}
Assert.AreEqual(expectedOutput, testOutput);
}
[TestMethod]
public void TestTruncateLast20BytesRead3BytesAtATime()
{
string testInput = "This is a really really really really really long string, longer than all the othersnrit even has some carriage returns in it, etc.-- final 20 bytes --";
string expectedOutput = "This is a really really really really really long string, longer than all the othersnrit even has some carriage returns in it, etc.";
StringBuilder testOutputBuilder = new StringBuilder();
using (var testStream = new StreamWhichEndsBeforeFinal20Bytes(new MemoryStream(Encoding.ASCII.GetBytes(testInput))))
{
int bytesRead = 0;
do
{
byte[] buffer = new byte[3];
bytesRead = testStream.Read(buffer, 0, 3);
testOutputBuilder.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
} while (bytesRead > 0);
}
Assert.AreEqual(expectedOutput, testOutputBuilder.ToString());
}
}
流类别
public class StreamWhichEndsBeforeFinal20Bytes : Stream
{
private readonly Stream sourceStream;
private static int TailBytesCount = 20;
public StreamWhichEndsBeforeFinal20Bytes(Stream sourceStream)
{
this.sourceStream = sourceStream;
}
public byte[] TailBytes { get { return previousTailBuffer; } }
public override void Flush()
{
sourceStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return sourceStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
sourceStream.SetLength(value);
}
private byte[] previousTailBuffer;
public override int Read(byte[] buffer, int offset, int count)
{
byte[] tailBuffer = new byte[TailBytesCount];
int expectedBytesRead;
if (previousTailBuffer == null)
expectedBytesRead = count + TailBytesCount;
else
expectedBytesRead = count;
try
{
byte[] readBuffer = new byte[expectedBytesRead];
int actualBytesRead = sourceStream.Read(readBuffer, offset, expectedBytesRead);
if (actualBytesRead == 0) return 0;
if (actualBytesRead < TailBytesCount)
{
int pickPreviousByteCount = TailBytesCount - actualBytesRead;
if (previousTailBuffer != null)
{
int pickFromIndex = previousTailBuffer.Length - pickPreviousByteCount;
Array.Copy(previousTailBuffer, 0, buffer, offset, count);
Array.Copy(previousTailBuffer, pickFromIndex, tailBuffer, 0, pickPreviousByteCount);
}
Array.Copy(readBuffer, 0, tailBuffer, pickPreviousByteCount, actualBytesRead);
return actualBytesRead;
}
Array.Copy(readBuffer, actualBytesRead - TailBytesCount, tailBuffer, 0, TailBytesCount);
Array.Copy(readBuffer, 0, buffer, offset, actualBytesRead - TailBytesCount);
if (actualBytesRead < expectedBytesRead)
{
return actualBytesRead - TailBytesCount;
}
return count;
}
finally
{
previousTailBuffer = tailBuffer;
}
}
public override void Write(byte[] buffer, int offset, int count)
{
sourceStream.Write(buffer, offset, count);
}
public override bool CanRead
{
get { return sourceStream.CanRead; }
}
public override bool CanSeek
{
get { return sourceStream.CanSeek; }
}
public override bool CanWrite
{
get { return sourceStream.CanWrite; }
}
public override long Length
{
get
{
if (sourceStream.Length < TailBytesCount) return sourceStream.Length;
return sourceStream.Length - TailBytesCount;
}
}
public override long Position
{
get { return sourceStream.Position; }
set { sourceStream.Position = value; }
}
}