コード例 #1
0
        private static void AddNounWithAdjectiveGenerators(IRandomWordGenerator adjectiveGenerator, IRandomWordGenerator nounGenerator, Dictionary <int, IList <PasswordGeneratingChain> > operatorsByLength)
        {
            foreach (int nounLength in nounGenerator.WordLengths)
            {
                foreach (int adjectiveLength in adjectiveGenerator.WordLengths)
                {
                    int passwordLength = adjectiveLength + nounLength;

                    var passwordGeneratingChain = new PasswordGeneratingChain(
                        () => adjectiveGenerator.GetRandomWord(adjectiveLength),
                        () => nounGenerator.GetRandomWord(nounLength));

                    if (operatorsByLength.ContainsKey(passwordLength))
                    {
                        operatorsByLength[passwordLength].Add(passwordGeneratingChain);
                    }
                    else
                    {
                        operatorsByLength[passwordLength] = new List <PasswordGeneratingChain> {
                            passwordGeneratingChain
                        };
                    }
                }
            }
        }
コード例 #2
0
        public Password Generate(int length)
        {
            if (!this.operatorsByLength.ContainsKey(length))
            {
                throw new ArgumentOutOfRangeException($"Generator doesn't contain words with {length} length");
            }

            IList <PasswordGeneratingChain> operators = this.operatorsByLength[length];
            PasswordGeneratingChain         passwordGeneratingChain = PickRandomGenerator(operators);
            Password password = passwordGeneratingChain.Generate();

            return(password);
        }