Ejemplo n.º 1
0
 public RsaAlgorithm(RsaCngAdapter rsaCngAdapter)
 {
     RsaCngAdapter = rsaCngAdapter;
 }
Ejemplo n.º 2
0
        internal static RsaAlgorithm ResolveRsaAlgorithm(SecurityKey key, string algorithm, bool requirePrivateKey)
        {
            if (key == null)
            {
                return(null);
            }

            if (key is RsaSecurityKey rsaKey)
            {
                if (rsaKey.Rsa != null)
                {
#if NETSTANDARD1_4
                    return(new RsaAlgorithm(rsaKey.Rsa));
#else
                    if (rsaKey.Rsa is RSACryptoServiceProvider rsa)
                    {
                        return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsa)));
                    }
                    else if (RsaCngAdapter.IsRsaCngSupported())
                    {
                        return(new RsaAlgorithm(new RsaCngAdapter(rsaKey.Rsa)));
                    }
#endif
                }
                else
                {
#if NETSTANDARD1_4
                    var rsaAlgorithm = new RsaAlgorithm(RSA.Create());
                    rsaAlgorithm.Rsa.ImportParameters(rsaKey.Parameters);
#else
                    var rsaCapiProvider = new RSACryptoServiceProvider();
                    rsaCapiProvider.ImportParameters(rsaKey.Parameters);
                    var rsaAlgorithm = new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsaCapiProvider));
#endif
                    rsaAlgorithm.ShouldDispose = true;
                    return(rsaAlgorithm);
                }
            }

            if (key is X509SecurityKey x509Key)
            {
#if NETSTANDARD1_4
                if (requirePrivateKey)
                {
                    return(new RsaAlgorithm(x509Key.PrivateKey as RSA));
                }
                else
                {
                    return(new RsaAlgorithm(x509Key.PublicKey as RSA));
                }
#else
                if (requirePrivateKey)
                {
                    return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(x509Key.PrivateKey as RSACryptoServiceProvider)));
                }
                else
                {
                    return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(x509Key.PublicKey as RSACryptoServiceProvider)));
                }
#endif
            }

            if (key is JsonWebKey webKey && webKey.Kty == JsonWebAlgorithmsKeyTypes.RSA)
            {
#if NETSTANDARD1_4
                var rsaAlgorithm = new RsaAlgorithm(RSA.Create());
                rsaAlgorithm.Rsa.ImportParameters(webKey.CreateRsaParameters());
#else
                var rsaCapiProvider = new RSACryptoServiceProvider();
                rsaCapiProvider.ImportParameters(webKey.CreateRsaParameters());
                var rsaAlgorithm = new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsaCapiProvider));
#endif
                rsaAlgorithm.ShouldDispose = true;
                return(rsaAlgorithm);
            }

            return(null);
        }