public static Rsa Create(ReadOnlySpan <Byte> key, RSAKeyType type = DefaultRSAKeyType, RSAParameters?parameters = null)
{
Rsa rsa = parameters is null?Rsa.Create() : Rsa.Create(parameters.Value);
if (rsa is null)
{
throw new FactoryException("Unknown exception");
}
Int32 size = key.Length * 8;
try
{
rsa.KeySize = size;
}
catch (CryptographicException)
{
throw new ArgumentException($@"Invalid key size: {size}", nameof(key));
}
switch (type)
{
case RSAKeyType.RSA:
rsa.ImportRSAPrivateKey(key);
break;
case RSAKeyType.Pkcs8:
rsa.ImportPkcs8PrivateKey(key);
break;
case RSAKeyType.RSAPublic:
rsa.ImportRSAPublicKey(key);
break;
case RSAKeyType.SubjectPublic:
rsa.ImportSubjectPublicKeyInfo(key);
break;
default:
throw new NotSupportedException();
}
return(rsa);
}