Exemplo n.º 1
0
        public void Execute(string[] args)
        {
            try
            {
                options.ParseArguments(args);
            }
            catch (Exception)
            {
                Usage();
                return;
            }

            string infile = this.options.GetString("infile");
            BIO    bin    = Program.GetInFile(options.GetString("infile"));

            DH     dh;
            string inform = this.options["inform"] as string;

            if (inform == "PEM")
            {
                dh = DH.FromParametersPEM(bin);
            }
            else if (inform == "DER")
            {
                dh = DH.FromParametersDER(bin);
            }
            else
            {
                Usage();
                return;
            }

            if (this.options.IsSet("text"))
            {
                Console.WriteLine(dh);
            }

            if (this.options.IsSet("check"))
            {
                DH.CheckCode check = dh.Check();
                if ((check & DH.CheckCode.NotSuitableGenerator) != 0)
                {
                    Console.WriteLine("the g value is not a generator");
                }
                if ((check & DH.CheckCode.CheckP_NotPrime) != 0)
                {
                    Console.WriteLine("p value is not prime");
                }
                if ((check & DH.CheckCode.CheckP_NotSafePrime) != 0)
                {
                    Console.WriteLine("p value is not a safe prime");
                }
                if ((check & DH.CheckCode.UnableToCheckGenerator) != 0)
                {
                    Console.WriteLine("unable to check the generator value");
                }
                if (check == 0)
                {
                    Console.WriteLine("DH parameters appear to be ok");
                }
            }

            if (this.options.IsSet("code"))
            {
                Console.WriteLine("-code is currently not implemented.");
            }

            if (!this.options.IsSet("noout"))
            {
                string outfile = this.options["outfile"] as string;
                BIO    bout;
                bool   outmem = false;
                if (string.IsNullOrEmpty(outfile))
                {
                    bout   = BIO.MemoryBuffer();
                    outmem = true;
                }
                else
                {
                    bout = BIO.File(outfile, "w");
                }

                string outform = this.options["outform"] as string;
                if (outform == "DER")
                {
                    dh.WriteParametersDER(bout);
                }
                else if (outform == "PEM")
                {
                    dh.WriteParametersPEM(bout);
                }
                else
                {
                    Usage();
                    return;
                }

                if (outmem)
                {
                    Stream cout = Console.OpenStandardOutput();
                    ArraySegment <byte> segment = bout.ReadBytes((int)bout.NumberWritten);
                    cout.Write(segment.Array, segment.Offset, segment.Count);
                }
            }
        }