Files
hcs/Hcs.Client/Client/UniClient.cs

50 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GostCryptography.Gost_R3411;
using Hcs.Client.Api;
using Hcs.Client.Internal;
using System;
using System.Security.Cryptography.X509Certificates;
namespace Hcs.Client
{
/// <summary>
/// Универсальный клиент для вызова всех реализованных функций интеграции с ГИС ЖКХ
/// </summary>
public class UniClient : ClientBase
{
public NsiApi Nsi => new(this);
public NsiCommonApi NsiCommon => 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<X509Certificate2, bool> predicate)
{
return CertificateHelper.FindCertificate(predicate);
}
/// <summary>
/// Производит для потока хэш по алгоритму "ГОСТ Р 34.11-94" в строке binhex
/// </summary>
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;
}
}
}