using GostCryptography.Properties;
using System;
using System.Security.Cryptography;
namespace GostCryptography.Base
{
///
/// Класс вычисления цифровой подписи ГОСТ
///
public class GostSignatureFormatter : AsymmetricSignatureFormatter
{
///
/// Конструктор
///
public GostSignatureFormatter()
{
}
///
/// Конструктор
///
/// Закрытый ключ для вычисления цифровой подписи
///
///
public GostSignatureFormatter(AsymmetricAlgorithm privateKey) : this()
{
SetKey(privateKey);
}
private GostAsymmetricAlgorithm _privateKey;
///
public override void SetKey(AsymmetricAlgorithm privateKey)
{
if (privateKey == null)
{
throw ExceptionUtility.ArgumentNull(nameof(privateKey));
}
if (!(privateKey is GostAsymmetricAlgorithm gostPrivateKey))
{
if (privateKey.SignatureAlgorithm.IndexOf("gost", StringComparison.OrdinalIgnoreCase) < 0)
{
throw ExceptionUtility.ArgumentOutOfRange(nameof(privateKey), Resources.ShouldSupportGost3410);
}
gostPrivateKey = new GostExternalAsymmetricAlgorithm(privateKey);
}
_privateKey = gostPrivateKey;
}
///
public override void SetHashAlgorithm(string hashAlgorithmName)
{
}
///
public override byte[] CreateSignature(byte[] hash)
{
if (hash == null)
{
throw ExceptionUtility.ArgumentNull(nameof(hash));
}
var reverseSignature = _privateKey.CreateSignature(hash);
Array.Reverse(reverseSignature);
return reverseSignature;
}
}
}