Exemple #1
0
        static void Main(string[] args)
        {
            Options opts = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, opts))
            {
                try
                {
                    GenerateMyPassword(opts);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                    Console.WriteLine(opts.GetUsage());
                }
            }

            Console.ReadKey();
        }
Exemple #2
0
        static bool ShouldAvoidAmbiguous(Options options, char letter)
        {
            bool shouldAvoidAmbiguous = true;
            if (Boolean.TryParse(options.AvoidAmbiguous, out shouldAvoidAmbiguous))
            {
                if (shouldAvoidAmbiguous)
                {
                    Regex ambiguous = new Regex("[oO0l1]+");
                    return ambiguous.IsMatch(String.Format("{0}", letter));
                }
            }
            else
                ThrowInvalidOptionArgument(Options.commandOptionAvoidAmbiguous, options.AvoidAmbiguous);

            return false;
        }
Exemple #3
0
        static void GenerateMyPassword(Options options)
        {
            if (options.Length < 3 || options.Length > 20)
            {
                throw new GMPWException("Need a number between [3-20] as argument.");
            }

            List<KeyValuePair<int, String>> dulList = new List<KeyValuePair<int, String>>();
            dulList.Add(new KeyValuePair<int, String>(options.NumberOfDigit, Options.commandOptionDigit));
            dulList.Add(new KeyValuePair<int, String>(options.NumberOfUpper, Options.commandOptionUpper));
            dulList.Add(new KeyValuePair<int, String>(options.NumberOfLower, Options.commandOptionLower));
            dulList.Add(new KeyValuePair<int, String>(options.NumberOfSymbol, Options.commandOptionSymbol));
            dulList.Sort((x, y) => y.Key - x.Key);

            Dictionary<String, int> dulMap = new Dictionary<String, int>();

            Random rand = new Random(Environment.TickCount);
            int dulListIter = 0;
            dulList.ForEach(
                    delegate(KeyValuePair<int, String> x) {
                        int usedCapacity = dulMap.Aggregate(0, (sum, value) =>
                        {
                            sum += value.Value;
                            return sum;
                        });

                        if (usedCapacity > options.Length)
                            AssertFailure();

                        if (x.Key == -1)
                        {
                            int number = 0;
                            if (dulListIter == dulList.Count - 1)
                                number = options.Length - usedCapacity;
                            else
                                number = rand.Next(options.Length - usedCapacity);

                            dulMap.Add(x.Value, number);
                        }
                        else
                        {
                            if ((x.Key < -1) || (x.Key > options.Length - usedCapacity))
                                ThrowInvalidOptionArgument(x.Value, x.Key);

                            if (dulListIter == dulList.Count - 1)
                                if (usedCapacity + x.Key != options.Length)
                                    ThrowInvalidOptionArgument(Options.commandOptionLength, options.Length);

                            dulMap.Add(x.Value, x.Key);
                        }

                        ++ dulListIter;
                    }
                );

            String pwInfo = String.Format("numbers : {0}\nlower letters: {1}\nupper letters: {2}\nsymbol letters: {3}\n",
                                                        dulMap[Options.commandOptionDigit],
                                                        dulMap[Options.commandOptionLower],
                                                        dulMap[Options.commandOptionUpper],
                                                        dulMap[Options.commandOptionSymbol]);

            StringBuilder pwBuilder = new StringBuilder();
            while (dulMap[Options.commandOptionDigit] > 0 ||
                        dulMap[Options.commandOptionUpper] > 0 ||
                        dulMap[Options.commandOptionLower] > 0 ||
                        dulMap[Options.commandOptionSymbol] > 0)
            {
                int picker = rand.Next(4);
                switch (picker)
                {
                    case 0:
                        if (dulMap[Options.commandOptionDigit] > 0)
                        {
                            Char c = rand.Next(10).ToString().ElementAt(0);
                            if (!ShouldAvoidAmbiguous(options, c))
                            {
                                pwBuilder.Append(c);
                                --dulMap[Options.commandOptionDigit];
                            }
                        }
                        break;
                    case 1:
                        if (dulMap[Options.commandOptionLower] > 0)
                        {
                            Char c = (Char)(rand.Next(26) + 97);
                            if (!ShouldAvoidAmbiguous(options, c))
                            {
                                pwBuilder.Append(c);
                                -- dulMap[Options.commandOptionLower];
                            }
                        }
                        break;
                    case 2:
                        if (dulMap[Options.commandOptionUpper] > 0)
                        {
                            Char c = (Char)(rand.Next(26) + 65);
                            if (!ShouldAvoidAmbiguous(options, c))
                            {
                                pwBuilder.Append(c);
                                -- dulMap[Options.commandOptionUpper];
                            }
                        }
                        break;
                    case 3:
                        if (dulMap[Options.commandOptionSymbol] > 0)
                        {
                            String symbols = "`~!@#$%^&*()_+-=[]{}\\|';\":/.,?><";
                            pwBuilder.Append(symbols.ElementAt(rand.Next(symbols.Length)));
                            -- dulMap[Options.commandOptionSymbol];
                        }
                        break;
                    default:
                        AssertFailure();
                        break;
                }
            }

            myPassword = pwBuilder.ToString();
            Console.WriteLine(pwInfo);
            Console.WriteLine(myPassword);
        }