using System.Security;
using GostCryptography.Base;
using GostCryptography.Native;
namespace GostCryptography.Gost_R3411
{
///
/// Базовый класс для всех реализаций алгоритма хэширования ГОСТ Р 34.11
///
public abstract class Gost_R3411_HashAlgorithm : GostHashAlgorithm, ISafeHandleProvider
{
///
[SecuritySafeCritical]
protected Gost_R3411_HashAlgorithm(int hashSize) : base(hashSize)
{
_hashHandle = CreateHashHandle();
}
///
[SecuritySafeCritical]
protected Gost_R3411_HashAlgorithm(ProviderType providerType, int hashSize) : base(providerType, hashSize)
{
_hashHandle = CreateHashHandle();
}
[SecurityCritical]
internal Gost_R3411_HashAlgorithm(ProviderType providerType, SafeProvHandleImpl providerHandle, int hashSize) : base(providerType, hashSize)
{
_hashHandle = CreateHashHandle(providerHandle);
}
///
/// Создает дескриптор функции хэширования криптографического провайдера
///
[SecurityCritical]
protected SafeHashHandleImpl CreateHashHandle()
{
return CreateHashHandle(CryptoApiHelper.GetProviderHandle(ProviderType));
}
///
/// Создает дескриптор функции хэширования криптографического провайдера
///
[SecurityCritical]
protected abstract SafeHashHandleImpl CreateHashHandle(SafeProvHandleImpl providerHandle);
[SecurityCritical]
private SafeHashHandleImpl _hashHandle;
///
SafeHashHandleImpl ISafeHandleProvider.SafeHandle
{
[SecurityCritical]
get => _hashHandle;
}
///
[SecuritySafeCritical]
public override void Initialize()
{
_hashHandle.TryDispose();
_hashHandle = CreateHashHandle();
}
///
[SecuritySafeCritical]
protected override void HashCore(byte[] data, int dataOffset, int dataLength)
{
CryptoApiHelper.HashData(_hashHandle, data, dataOffset, dataLength);
}
///
[SecuritySafeCritical]
protected override byte[] HashFinal()
{
return CryptoApiHelper.EndHashData(_hashHandle);
}
///
[SecuritySafeCritical]
protected override void Dispose(bool disposing)
{
_hashHandle.TryDispose();
base.Dispose(disposing);
}
}
}