Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions DFe.Utils/Assinatura/CertificadoDigital.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand All @@ -43,7 +43,7 @@
{
public static class CertificadoDigital
{
private static readonly Dictionary<string, X509Certificate2> CacheCertificado = new Dictionary<string, X509Certificate2>();
private static readonly ConcurrentDictionary<string, X509Certificate2> CacheCertificado = new ConcurrentDictionary<string, X509Certificate2>();

#region Métodos privados

Expand Down Expand Up @@ -153,7 +153,7 @@
if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.Win32S)
{
if (certificado == null) throw new ArgumentNullException("certificado");
var key = (RSACryptoServiceProvider)certificado.PrivateKey;

Check warning on line 156 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.'

Check warning on line 156 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.'

var providerHandle = IntPtr.Zero;
var pinBuffer = Encoding.ASCII.GetBytes(pin);
Expand Down Expand Up @@ -204,29 +204,29 @@
/// <summary>
/// Obtém um objeto contendo o certificado digital
/// <para>Se for informado <see cref="ConfiguracaoCertificado.Arquivo"/>,
/// o certificado digital será obtido pelo método <see cref="ObterDeArquivo(string,string)"/>,
/// senão será obtido pelo método <see cref="ListareObterDoRepositorio"/> </para>
/// o certificado digital será obtido pelo método <see cref="ObterDeArquivo(string,string,X509KeyStorageFlags)"/>,
/// senão será obtido pelo método <see cref="ObterDoRepositorio"/> </para>
/// <para>Para liberar os recursos do certificado, após seu uso, invoque o método <see cref="X509Certificate2.Reset()"/></para>
/// </summary>
public static X509Certificate2 ObterCertificado(ConfiguracaoCertificado configuracaoCertificado)
{
if (!configuracaoCertificado.ManterDadosEmCache)
return ObterDadosCertificado(configuracaoCertificado);

if (!string.IsNullOrEmpty(configuracaoCertificado.CacheId) && CacheCertificado.ContainsKey(configuracaoCertificado.CacheId))
return CacheCertificado[configuracaoCertificado.CacheId];
if (!string.IsNullOrWhiteSpace(configuracaoCertificado.CacheId) && CacheCertificado.TryGetValue(configuracaoCertificado.CacheId, out var certificadoEmCache))
return certificadoEmCache;

var certificado = ObterDadosCertificado(configuracaoCertificado);

var keyCertificado = string.IsNullOrEmpty(configuracaoCertificado.CacheId)
var keyCertificado = string.IsNullOrWhiteSpace(configuracaoCertificado.CacheId)
? certificado.SerialNumber
: configuracaoCertificado.CacheId;

configuracaoCertificado.CacheId = keyCertificado;

var certificadoDoCache = CacheCertificado.GetOrAdd(keyCertificado, certificado);

CacheCertificado.Add(keyCertificado, certificado);

return CacheCertificado[keyCertificado];
return certificadoDoCache;
}

public static void ClearCache()
Expand Down Expand Up @@ -338,7 +338,7 @@

try
{
RSACryptoServiceProvider service = x509Certificate2.PrivateKey as RSACryptoServiceProvider;

Check warning on line 341 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.'

Check warning on line 341 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.'

if (service != null)
{
Expand Down
Loading