提交 956b17ef 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #19313 from CyrusNajmabadi/poolSha1Instances

Pool byte array used for reading from Stream during checksum calculation
......@@ -16,11 +16,33 @@ internal partial class Checksum
{
public static Checksum Create(Stream stream)
{
// REVIEW: should we cache SHA1CryptoServiceProvider
using (var algorithm = SHA1.Create())
using (var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
{
return ComputeChecksum(stream, hash);
}
}
private static Checksum ComputeChecksum(Stream stream, IncrementalHash hash)
{
using (var pooledBuffer = SharedPools.ByteArray.GetPooledObject())
{
stream.Seek(0, SeekOrigin.Begin);
return new Checksum(algorithm.ComputeHash(stream));
var buffer = pooledBuffer.Object;
var bufferLength = buffer.Length;
int bytesRead;
do
{
bytesRead = stream.Read(buffer, 0, bufferLength);
if (bytesRead > 0)
{
hash.AppendData(buffer, 0, bytesRead);
}
}
while (bytesRead > 0);
var bytes = hash.GetHashAndReset();
return new Checksum(bytes);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册