Example #1
0
        public GeneratorOptions GetOptions(GeneratorOptions defaults)
        {
            PasswordLengthNumericUpDown.Value = defaults.WordLength;
            SubsituteCheckBox.Checked = defaults.SubstituteCharacters;
            SubstitutionComboBox.Text = defaults.SubstitutionList;
            CapitalizationComboBox.SelectedIndex = (int)defaults.CapitalizationType;

            if (this.ShowDialog() != DialogResult.OK) return defaults;

            GeneratorOptions options = new GeneratorOptions();
            options.WordLength = Convert.ToInt32(PasswordLengthNumericUpDown.Value);
            options.SubstituteCharacters = SubsituteCheckBox.Checked;
            options.SubstitutionList = SubstitutionComboBox.Text;
            options.CapitalizationType = (CapitalizationTypes)CapitalizationComboBox.SelectedIndex;

            return options;
        }
        /// <summary>
        /// Returns a set of options associated with this generator.
        /// </summary>
        /// <param name="strCurrentOptions">The set of current options.</param>
        /// <returns>The set of new options.</returns>
        public override string GetOptions(string strCurrentOptions)
        {
            GeneratorOptions options = new GeneratorOptions(strCurrentOptions);

            // Open the option dialog to generate new options.
            OptionDialog optionsDialog = new OptionDialog();
            options = optionsDialog.GetOptions(options);
            optionsDialog.Dispose();

            return options.ToString();
        }
        /// <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);
        }