HashAlgorithmHasher
HashAlgorithm wrapper for IHasher interface
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Storage
{
internal class HashAlgorithmHasher : IHasher, IDisposable
{
private readonly HashAlgorithm _hashAlgorithm;
public int HashSizeInBytes => BitsToBytes(_hashAlgorithm.HashSize);
public HashAlgorithmHasher(HashAlgorithm hashAlgorithm)
{
_hashAlgorithm = hashAlgorithm;
}
[AsyncStateMachine(typeof(<ComputeHashInternal>d__4))]
public Task<byte[]> ComputeHashInternal(Stream stream, bool async, CancellationToken cancellationToken)
{
<ComputeHashInternal>d__4 stateMachine = default(<ComputeHashInternal>d__4);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<byte[]>.Create();
stateMachine.<>4__this = this;
stateMachine.stream = stream;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public void AppendHash(ReadOnlySpan<byte> content)
{
_hashAlgorithm.TransformBlock(content.ToArray(), 0, content.Length, null, 0);
}
public int GetFinalHash(Span<byte> hashDestination)
{
_hashAlgorithm.TransformFinalBlock(new byte[0], 0, 0);
_hashAlgorithm.Hash.CopyTo(hashDestination);
return _hashAlgorithm.Hash.Length;
}
public void Dispose()
{
_hashAlgorithm.Dispose();
}
private static int BitsToBytes(int bits)
{
return bits >> 3;
}
}
}