using GostCryptography.Gost_R3411;
using Hcs.Client.Api;
using Hcs.Client.Internal;
using System;
using System.Security.Cryptography.X509Certificates;
namespace Hcs.Client
{
///
/// Универсальный клиент для вызова всех реализованных функций интеграции с ГИС ЖКХ
///
public class UniClient : ClientBase
{
public NsiApi Nsi => new(this);
public void SetSigningCertificate(X509Certificate2 cert, string pin = null)
{
pin ??= Constants.DEFAULT_CERTIFICATE_PIN;
Certificate = cert ?? throw new ArgumentNullException("Certificate not specified");
CryptoProviderType = cert.GetProviderType();
CertificateThumbprint = cert.Thumbprint;
CertificatePassword = pin;
}
public X509Certificate2 FindCertificate(Func predicate)
{
return CertificateHelper.FindCertificate(predicate);
}
///
/// Производит для потока хэш по алгоритму "ГОСТ Р 34.11-94" в строке binhex
///
public string ComputeGost94Hash(System.IO.Stream stream)
{
// API HouseManagement указывает, что файлы приложенные к договору должны размещаться
// с AttachmentHASH по стандарту ГОСТ. Оказывается, ГИС ЖКХ требует применения устаревшего
// алгоритма ГОСТ Р 34.11-94 (соответствует `rhash --gost94-cryptopro file` в linux).
using var algorithm = new Gost_R3411_94_HashAlgorithm(GostCryptoProviderType);
var savedPosition = stream.Position;
stream.Position = 0;
var hashValue = Util.ConvertToHexString(algorithm.ComputeHash(stream));
stream.Position = savedPosition;
return hashValue;
}
}
}