예제 #1
0
        static void Main(string[] args)
        {
            Console.Title = "Password-Generator: Demo";

Regenerate:

            Console.WriteLine(PasswordGenerator.Generate());

            Console.ReadKey();

            Console.Clear();

            goto Regenerate;
        }
        //--------------------------------------------------
        // fill the pgo struct and initialize PasswordGenerator class with it
        static void ProcessCmdline()
        {
            // password length
            if (!CommandLineArgs.ContainsKey("--length"))
            {
                ReportErrorAndExit("ERROR: required argument is absent: --length", ExitCodes.cmdlineArgError);
            }
            if (!int.TryParse(CommandLineArgs["--length"].ToString(), out pgo.pswLength) || pgo.pswLength <= 0)
            {
                ReportErrorAndExit("ERROR: incorrect argument value: --length:" + CommandLineArgs["--length"].ToString(), ExitCodes.cmdlineArgError);
            }

            // quantity
            if (!CommandLineArgs.ContainsKey("--quantity"))
            {
                ReportErrorAndExit("ERROR: required argument is absent: --quantity", ExitCodes.cmdlineArgError);
            }
            if (!int.TryParse(CommandLineArgs["--quantity"].ToString(), out pgo.quantity) || pgo.quantity <= 0)
            {
                ReportErrorAndExit("ERROR: incorrect argument value: --quantity:" + CommandLineArgs["--quantity"].ToString(), ExitCodes.cmdlineArgError);
            }

            // charsets
            if (!CommandLineArgs.ContainsKey("--charsets"))
            {
                ReportErrorAndExit("ERROR: required argument is absent: --charsets", ExitCodes.cmdlineArgError);
            }
            string charsets = CommandLineArgs["--charsets"].ToString();
            if (charsets.Length == 0)
            {
                ReportErrorAndExit("ERROR: required argument value is empty: --charsets", ExitCodes.cmdlineArgError);
            }
            // fill the pgo.charsets array
            pgo.charsets = new string[0];
            Hashtable cs;
            cs = pswgen.Charsets;
            foreach (string charset in charsets.Split(','))
            {
                if (charset.Length == 0)
                {
                    ReportErrorAndExit("ERROR: incorrect argument value: --charsets:" + charsets, ExitCodes.cmdlineArgError);
                }
                if (!cs.ContainsKey(charset))
                {
                    ReportErrorAndExit("ERROR: unknown charset in the \"--charsets\" argument: " + charset, ExitCodes.cmdlineArgError);
                }
                Array.Resize(ref pgo.charsets, pgo.charsets.Length + 1);
                pgo.charsets[pgo.charsets.Length - 1] = charset;
            }

            // user defined charset
            if (pgo.charsets.Contains("userDefined"))
            {
                if(!CommandLineArgs.ContainsKey("--userDefinedCharset"))
                {
                    ReportErrorAndExit("ERROR: required argument is absent: --userDefinedCharset", ExitCodes.cmdlineArgError);
                }
                string udc = CommandLineArgs["--userDefinedCharset"].ToString();
                if (udc.Length == 0)
                {
                    ReportErrorAndExit("ERROR: argument value is empty: --userDefinedCharset", ExitCodes.cmdlineArgError);
                }
                pgo.userDefinedCharset = udc;
            }

            // generate easy-to-type passwords
            if (CommandLineArgs.ContainsKey("--easytotype"))
            {
                string ett = CommandLineArgs["--easytotype"].ToString();
                if (ett.Length != 0)
                {
                    ReportErrorAndExit("ERROR: incorrect argument value (must be empty!): --easytotype:" + ett, ExitCodes.cmdlineArgError);
                }
                pgo.easyToType = true;
            }

            // exclude confusing characters
            if (CommandLineArgs.ContainsKey("--excludeConfusingCharacters"))
            {
                string ecc = CommandLineArgs["--excludeConfusingCharacters"].ToString();
                if (ecc.Length != 0)
                {
                    ReportErrorAndExit("ERROR: incorrect argument value (must be empty!): --excludeConfusingCharacters:" + ecc, ExitCodes.cmdlineArgError);
                }
                pgo.excludeConfusing = true;
            }

            // destination
            if (!CommandLineArgs.ContainsKey("--destination"))
            {
                ReportErrorAndExit("ERROR: required argument is absent: --destination", ExitCodes.cmdlineArgError);
            }
            string dest = CommandLineArgs["--destination"].ToString();
            if (dest.Length == 0)
            {
                ReportErrorAndExit("ERROR: required argument value is empty: --destination", ExitCodes.cmdlineArgError);
            }
            string destType = GetLongArgOrValueName(dest.Split(':')[0]);
            switch (destType)
            {
                case "console":
                    destinationType = DestinationType.console;
                    break;
                case "file":
                    if (dest.Split(':').Length < 3)
                    {
                        ReportErrorAndExit("ERROR: incorrect destination: --destination:" + dest, ExitCodes.cmdlineArgError);
                    }

                    string destFileAccessMethod = GetLongArgOrValueName(dest.Split(':')[1]);
                    switch (destFileAccessMethod)
                    {
                        case "append":
                            appendToFile = true;
                            break;
                        case "replace":
                            appendToFile = false;
                            break;
                        default:
                            ReportErrorAndExit("ERROR: unknown file access method: --destination:" + dest, ExitCodes.cmdlineArgError);
                            break;
                    }

                    //fileName;

                    int pos = dest.IndexOf(':', 0);
                    pos = dest.IndexOf(':', pos + 1);
                    fileName = dest.Substring(pos + 1);
                    if (fileName.Length == 0)
                    {
                        ReportErrorAndExit("ERROR: file name is empty: --destination:" + dest, ExitCodes.cmdlineArgError);
                    }

                    destinationType = DestinationType.file;
                    break;
                default:
                    ReportErrorAndExit("ERROR: unknown destination type: --destination:" + dest.Split(':')[0], ExitCodes.cmdlineArgError);
                    break;
            }

            // initialize class
            pswgen = new PasswordGenerator(pgo);
            if (!pswgen.isReady)
            {
                ReportErrorAndExit("ERROR: some errors in command line arguments", ExitCodes.cmdlineArgError);
            }
        }
예제 #3
0
        void Entry()
        {
            _PasswordGenerator = new PasswordGenerator();

            StartProgram();
        }
        //--------------------------------------------------
        public void PrepareToGeneration(genType gt, pswType pt, ref PasswordGenerator pg)
        {
            PasswordGenerator.PasswordGenerationOptions pgo = new PasswordGenerator.PasswordGenerationOptions();

            // options
            switch (gt)
            {
                case genType.Single:
                    pgo.easyToType = (bool)chkSingle_EasyToType.IsChecked;
                    pgo.excludeConfusing = (bool)chkSingle_ExcludeConfusingChars.IsChecked;
                    pgo.quantity = 1;
                    break;
                case genType.Bulk:
                    pgo.easyToType = (bool)chkBulkEasyToType.IsChecked;
                    pgo.excludeConfusing = (bool)chkBulkExcludeConfusingChars.IsChecked;
                    pgo.quantity = udBulkQuantity.Value ?? 0;
                    fileName = txtBulkFile.Text;
                    appendToFile = rbBulkAppend.IsChecked ?? true;
                    break;
                default:
                    pgo.pswLength = 0;  // mark structure as invalid
                    return;   // error
            }

            // fill the pgo.charsets array
            pgo.charsets = new string[0];

            switch (pt)
            {
                case pswType.Password:
                    pgo.pswLength = udPswLength.Value ?? 0;

                    // search for all checkboxes in the expPassword expander
                    foreach (object ctrl in FindAllChildren(expPassword))
                        if (ctrl.GetType().Name == "CheckBox" && (bool)((CheckBox)ctrl).IsChecked && ((CheckBox)ctrl).Tag != null)
                        {
                            Array.Resize(ref pgo.charsets, pgo.charsets.Length + 1);
                            pgo.charsets[pgo.charsets.Length - 1] = (string)((CheckBox)ctrl).Tag;
                        }

                    // user defined charset
                    if ((bool)chkPswUserDefinedCharacters.IsChecked)
                        pgo.userDefinedCharset = txtPswUserDefinedCharacters.Text;

                    break;
                case pswType.WPA:
                    if ((bool)rbWpaPassphrase.IsChecked)
                    {
                        pgo.pswLength = udWpaLength.Value ?? 0;

                        // search for all checkboxes in the expWPA expander
                        foreach (object ctrl in FindAllChildren(expWPA))
                            if (ctrl.GetType().Name == "CheckBox" && (bool)((CheckBox)ctrl).IsChecked && ((CheckBox)ctrl).Tag != null)
                            {
                                Array.Resize(ref pgo.charsets, pgo.charsets.Length + 1);
                                pgo.charsets[pgo.charsets.Length - 1] = (string)((CheckBox)ctrl).Tag;
                            }

                            // user defined charset
                        if ((bool)chkWpaUserDefinedCharacters.IsChecked)
                            pgo.userDefinedCharset = txtWpaUserDefinedCharacters.Text;
                    }
                    else  // rbWpa256bitKey is checked
                    {
                        pgo.pswLength = 64;

                        // use only one charset
                        pgo.charsets = new string[1];
                        pgo.charsets[0] = "hex";
                    }
                    break;
                default:
                    pgo.pswLength = 0;
                    return; // some error
            }

            pg = new PasswordGenerator(pgo);    // create and initialize the password generator class
        }