private IEnumerable <string> SelectPasswords(IRandomNumberGenerator random, int syllableCount, int count, bool hyphansBetweenSyllables)
        {
            syllableCount = Math.Min(syllableCount, MaxSyllableCount);
            count         = Math.Min(count, MaxCount);
            if (count <= 0 || syllableCount <= 0)
            {
                yield break;
            }

            var sw = System.Diagnostics.Stopwatch.StartNew();
            var sb = new StringBuilder();

            for (int c = 0; c < count; c++)
            {
                // Generate a password.
                for (int l = 0; l < syllableCount; l++)
                {
                    sb.Append(ConsonantSounds[random.GetRandomInt32(ConsonantSounds.Length)]);
                    sb.Append(VowelSounds[random.GetRandomInt32(VowelSounds.Length)]);
                    if (sb[sb.Length - 2] != 'g' && sb[sb.Length - 1] != 'h' &&
                        random.GetRandomSingle() < ProbabilityOfTwoConsonantsInOneSyllable)
                    {
                        sb.Append(ConsonantSounds[random.GetRandomInt32(ConsonantSounds.Length)]);
                    }

                    if (hyphansBetweenSyllables)
                    {
                        sb.Append('-');
                    }
                }
                if (hyphansBetweenSyllables && sb[sb.Length - 1] == '-')
                {
                    sb.Remove(sb.Length - 1, 1);
                }


                // Yield the phrase and reset state.
                var result = sb.ToString();
                yield return(result);

                sb.Clear();
            }
            sw.Stop();

            var bytesRequested = (int)((random as Terninger.Random.CypherBasedPrngGenerator)?.BytesRequested).GetValueOrDefault();

            RandomService.LogPasswordStat("Pronouncable", count, sw.Elapsed, bytesRequested, IPAddressHelpers.GetHostOrCacheIp(Request).AddressFamily, HttpContext.GetApiKeyId());
            if (!IpThrottlerService.HasAnyUsage(IPAddressHelpers.GetHostOrCacheIp(this.HttpContext.Request)))
            {
                RandomService.AddWebRequestEntropy(this.Request);
            }
            IpThrottlerService.IncrementUsage(IPAddressHelpers.GetHostOrCacheIp(this.HttpContext.Request), count);
        }
Пример #2
0
        private IEnumerable <string> SelectPasswords(IRandomNumberGenerator random, int syllableCount, int count, bool hyphansBetweenSyllables)
        {
            syllableCount = Math.Min(syllableCount, MaxSyllableCount);
            count         = Math.Min(count, MaxCount);
            if (count <= 0 || syllableCount <= 0)
            {
                yield break;
            }

            var sw = System.Diagnostics.Stopwatch.StartNew();
            var sb = new StringBuilder();

            for (int c = 0; c < count; c++)
            {
                // Generate a password.
                for (int l = 0; l < syllableCount; l++)
                {
                    sb.Append(ConsonantSounds[random.GetRandomInt32(ConsonantSounds.Length)]);
                    sb.Append(VowelSounds[random.GetRandomInt32(VowelSounds.Length)]);
                    if (sb[sb.Length - 2] != 'g' && sb[sb.Length - 1] != 'h' &&
                        random.GetRandomSingle() < ProbabilityOfTwoConsonantsInOneSyllable)
                    {
                        sb.Append(ConsonantSounds[random.GetRandomInt32(ConsonantSounds.Length)]);
                    }

                    if (hyphansBetweenSyllables)
                    {
                        sb.Append('-');
                    }
                }
                if (hyphansBetweenSyllables && sb[sb.Length - 1] == '-')
                {
                    sb.Remove(sb.Length - 1, 1);
                }


                // Yield the phrase and reset state.
                var result = sb.ToString();
                yield return(result);

                sb.Clear();
            }
            sw.Stop();

            PostSelectionAction("Pronounceable", count, sw.Elapsed, random);
        }