Ejemplo n.º 1
0
        /// <summary>
        /// Capitalizes characters on the password.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="capitalizationType">The capitalization type.</param>
        /// <returns>The capitalized password.</returns>
        private static string CapitalizePassword(string password, CapitalizationTypes capitalizationType, RandomNumber randomNumber)
        {
            if (capitalizationType == CapitalizationTypes.None)
                return password;

            char[] passwordChars = password.ToCharArray();

            if (capitalizationType == CapitalizationTypes.FirstLetter)
            {
                // Loop through charactes and make the first letter upercase.
                for (int i = 0; i < passwordChars.Length; i++)
                {
                    if (char.IsLetter(passwordChars[i]) && char.IsLower(passwordChars[i]))
                    {
                        passwordChars[i] = char.ToUpper(passwordChars[i]);
                        break;
                    }
                }
            }
            else
            {
                // First let's make sure this can be applied to the current string
                bool hasLetter = false;
                for (int i = 0; i < passwordChars.Length; i++)
                {
                    if (char.IsLetter(passwordChars[i]))
                    {
                        hasLetter = true;
                        break;
                    }
                }

                // Get a random letter from the password and make it uppercase
                if (hasLetter)
                {
                    while (true)
                    {
                        int i = randomNumber.Next(passwordChars.Length);
                        if (char.IsLetter(passwordChars[i]) && char.IsLower(passwordChars[i]))
                        {
                            passwordChars[i] = char.ToUpper(passwordChars[i]);
                            break;
                        }
                    }
                }
            }

            return new string(passwordChars);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate a random dictionary password.
        /// </summary>
        /// <param name="prf">The password profile to use.</param>
        /// <param name="crsRandomSource">The cryptographic stream.</param>
        /// <returns>A generated ProtectedString password.</returns>
        public override ProtectedString Generate(PwProfile prf, CryptoRandomStream crsRandomSource)
        {
            // Get the generator options.
            GeneratorOptions options = new GeneratorOptions(prf.CustomAlgorithmOptions);

            // Check if a word dictionary has already been loaded, if not, load it.
            if (_wordDictionary == null || _currentWordLength != options.WordLength)
            {
                _wordDictionary = ExtractWordDictionary(options.WordLength);
                _currentWordLength = options.WordLength;
            }

            // Get a random word from the dictionary
            RandomNumber randomNumber = new RandomNumber(crsRandomSource);
            string password = _wordDictionary.Count > 0 ? _wordDictionary[randomNumber.Next(_wordDictionary.Count)] : string.Empty;
            _wordDictionary.Remove(password);

            // Substitute characters if specified.
            if (options.SubstituteCharacters && !string.IsNullOrEmpty(options.SubstitutionList))
                password = SubstituteCharacters(password, options.SubstitutionList);

            // Capitalize if necessary
            if (options.CapitalizationType != CapitalizationTypes.None)
                password = CapitalizePassword(password, options.CapitalizationType, randomNumber);

            return new ProtectedString(false, password);
        }