예제 #1
0
        public String GeneratePassword(CombinedKeyOptions options)
        {
            if ((options.PassphraseGenerationOptions == null || options.PassphraseGenerationOptions.WordCount == 0) &&
                (options.PasswordGenerationOptions == null || options.PasswordGenerationOptions.Length == 0))
            {
                throw new Exception("Bad options");
            }

            string key = "";

            Random random = new SecureRandom();

            var passwordOptions   = options.PasswordGenerationOptions;
            var passphraseOptions = options.PassphraseGenerationOptions;

            if (passphraseOptions != null && passphraseOptions.WordCount > 0)
            {
                var    wl         = new Wordlist();
                string passphrase = "";
                for (int i = 0; i < passphraseOptions.WordCount; i++)
                {
                    string word = wl.GetWord(random);

                    if (passphraseOptions.CaseMode == PassphraseGenerationOptions.PassphraseCaseMode.Uppercase)
                    {
                        word = word.ToUpper();
                    }
                    else if (passphraseOptions.CaseMode == PassphraseGenerationOptions.PassphraseCaseMode.Lowercase)
                    {
                        word = word.ToLower();
                    }
                    else if (passphraseOptions.CaseMode == PassphraseGenerationOptions.PassphraseCaseMode.PascalCase)
                    {
                        word = word.ToUpperFirstLetter();
                    }

                    passphrase += word;

                    if (i < passphraseOptions.WordCount - 1 || passwordOptions != null)
                    {
                        passphrase += passphraseOptions.Separator;
                    }
                }

                key += passphrase;
            }


            if (passwordOptions != null)
            {
                var    groups       = GetCharacterGroups(passwordOptions);
                String characterSet = GetCharacterSet(passwordOptions, groups);

                if (characterSet.Length == 0)
                {
                    throw new Exception("Bad options");
                }

                int size = characterSet.Length;

                StringBuilder buffer = new StringBuilder();

                if (passwordOptions.AtLeastOneFromEachGroup)
                {
                    foreach (var g in groups)
                    {
                        if (g.Length > 0)
                        {
                            buffer.Append(g[random.Next(g.Length)]);
                        }
                    }
                }

                if (size > 0)
                {
                    while (buffer.Length < passwordOptions.Length)
                    {
                        buffer.Append(characterSet[random.Next(size)]);
                    }
                }

                var password = buffer.ToString();

                if (passwordOptions.AtLeastOneFromEachGroup)
                {
                    //shuffle
                    StringBuilder sb = new StringBuilder(password);
                    for (int i = (password.Length - 1); i >= 1; i--)
                    {
                        int j = random.Next(i + 1);

                        var tmp = sb[i];
                        sb[i] = sb[j];
                        sb[j] = tmp;
                    }

                    password = sb.ToString();
                }

                key += password;
            }


            return(key);
        }