Beispiel #1
0
        public IEnumerable<String> Generate(ICommonSettings settings, IPoolsCollector collector)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (collector == null)
            {
                throw new ArgumentNullException(nameof(collector));
            }

            if (settings.Length < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(settings), $"Settings value {nameof(ICommonSettings.Length)} must be greater than zero.");
            }

            if (settings.Amount < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(settings), $"Settings value {nameof(ICommonSettings.Amount)} must be greater than zero.");
            }

            Pools pools = Pools.Nothing;

            if (settings.IsUppers) { pools |= Pools.Uppers; }
            if (settings.IsLowers) { pools |= Pools.Lowers; }
            if (settings.IsDigits) { pools |= Pools.Digits; }
            if (settings.IsExtras) { pools |= Pools.Extras; }

            String source = collector.Collect(pools);

            if (String.IsNullOrWhiteSpace(source))
            {
                return Enumerable.Empty<String>();
            }

            IEnumerable<String> results = null;

            switch (settings.Type)
            {
                case CommonType.PasswordManager1:
                case CommonType.PasswordManager2:
                case CommonType.PasswordManager3:
                    results = this.Generate(source, settings.Length, settings.Amount);
                    break;
                default:
                    throw new NotSupportedException($"Common type of \"{settings.Type}\" is not supported.");
            }

            this.DumpGeneratorResults(settings, results);

            return results;
        }
        private void ApplySourcesTypeX(IPoolsCollector collector, Int32 length, List <String> sources)
        {
            Int32[] counts = this.CalculateCounts(length);

            for (Int32 index = 0; index < counts[0]; index++)
            {
                if (index == 0)
                {
                    sources.Add(collector.Consonants.Shuffle(this.random).ToUpper());
                }
                else if (index % 2 != 0)
                {
                    sources.Add(collector.Vowels.Shuffle(this.random));
                }
                else
                {
                    sources.Add(collector.Consonants.Shuffle(this.random));
                }
            }

            for (Int32 index = 0; index < counts[1]; index++)
            {
                sources.Add(collector.Collect(Pools.Symbols | Pools.Operators | Pools.Punctuations | Pools.Spaces).Shuffle(this.random));
            }

            for (Int32 index = 0; index < counts[2]; index++)
            {
                if (index == 0)
                {
                    sources.Add(collector.Vowels.Shuffle(this.random).ToUpper());
                }
                else if (index % 2 != 0)
                {
                    sources.Add(collector.Consonants.Shuffle(this.random));
                }
                else
                {
                    sources.Add(collector.Vowels.Shuffle(this.random));
                }
            }

            for (Int32 index = 0; index < counts[3]; index++)
            {
                sources.Add(collector.Digits.Shuffle(this.random));
            }
        }
        private void ApplySourcesType3(IPoolsCollector collector, Int32 length, List <String> sources)
        {
            // TODO: Change algorithm so that it better fits the rules: Cvcv[0..n/2-1]XVcvc[n/-1..n-3]DDD
            // C = consonant; V = Vowel; X = Extra; D = Digit

            Int32 count = 0;
            Int32 index = 0;

            sources.Add(collector.Consonants.Shuffle(this.random).ToUpper());
            index++;

            if (index >= length)
            {
                return;
            }

            count = (length - 3) / 2;

            for (; index < count; index++)
            {
                if (index % 2 != 0)
                {
                    sources.Add(collector.Vowels.Shuffle(this.random));
                }
                else
                {
                    sources.Add(collector.Consonants.Shuffle(this.random));
                }
            }

            if (index >= length)
            {
                return;
            }

            sources.Add(collector.Collect(Pools.Symbols | Pools.Operators | Pools.Punctuations | Pools.Spaces).Shuffle(this.random));
            index++;

            if (index >= length)
            {
                return;
            }

            sources.Add(collector.Vowels.Shuffle(this.random).ToUpper());
            index++;

            if (index >= length)
            {
                return;
            }

            count = length - 3;

            for (; index < count; index++)
            {
                if (index % 2 == 0)
                {
                    sources.Add(collector.Vowels.Shuffle(this.random));
                }
                else
                {
                    sources.Add(collector.Consonants.Shuffle(this.random));
                }
            }

            if (index >= length)
            {
                return;
            }

            count = length;

            for (; index < count; index++)
            {
                sources.Add(collector.Digits.Shuffle(this.random));
            }
        }