예제 #1
0
        protected override object OnExecute(CommandContext context)
        {
            var rsa = RSACommand.FindRSA(context.CommandNode);

            if (rsa == null)
            {
                throw new CommandException("Missing the required RSA.");
            }

            switch (context.Expression.Options.GetValue <RSAKeyType>(TYPE_OPTION))
            {
            case RSAKeyType.All:
                return(rsa.ToXmlString(true));

            case RSAKeyType.Public:
                return(rsa.ExportRSAPublicKey());

            case RSAKeyType.Private:
                return(context.Expression.Options.GetValue <RSAKeyFormat>(FORMAT_OPTION) == RSAKeyFormat.Pkcs8 ?
                       rsa.ExportPkcs8PrivateKey() :
                       rsa.ExportRSAPrivateKey());

            case RSAKeyType.Subject:
                return(rsa.ExportSubjectPublicKeyInfo());

            default:
                return(rsa.ExportSubjectPublicKeyInfo());
            }
        }
예제 #2
0
        protected override object OnExecute(CommandContext context)
        {
            var rsa = RSACommand.FindRSA(context.CommandNode);

            if (rsa == null)
            {
                throw new CommandException("Missing the required RSA.");
            }

            switch (context.Expression.Options.GetValue <RSAKeyType>(TYPE_OPTION))
            {
            case RSAKeyType.All:
                if (TryGetInputXml(context.Parameter, out var xml))
                {
                    rsa.FromXmlString(xml);
                }
                break;

            case RSAKeyType.Public:
                if (TryGetInput(context.Parameter, out var publicKey))
                {
                    rsa.ImportRSAPublicKey(publicKey, out _);
                }
                break;

            case RSAKeyType.Subject:
                if (TryGetInput(context.Parameter, out var subject))
                {
                    rsa.ImportSubjectPublicKeyInfo(subject, out _);
                }
                break;

            case RSAKeyType.Private:
                if (TryGetInput(context.Parameter, out var privateKey))
                {
                    if (context.Expression.Options.GetValue <RSAKeyFormat>(FORMAT_OPTION) == RSAKeyFormat.Pkcs8)
                    {
                        rsa.ImportPkcs8PrivateKey(privateKey, out _);
                    }
                    else
                    {
                        rsa.ImportRSAPrivateKey(privateKey, out _);
                    }
                }
                break;
            }

            return(rsa);
        }