Пример #1
0
        public static int GetTotalCharCountForSelection(CharSelection selection)
        {
            int totalCount = 0;

            if (selection.HasFlag(CharSelection.Lowercase))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_LOWERCASE.Length;
            }
            if (selection.HasFlag(CharSelection.Uppercase))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_UPPERCASE.Length;
            }
            if (selection.HasFlag(CharSelection.Digits))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_DIGITS.Length;
            }
            if (selection.HasFlag(CharSelection.Minus))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_MINUS.Length;
            }
            if (selection.HasFlag(CharSelection.Underline))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_UNDERLINE.Length;
            }
            if (selection.HasFlag(CharSelection.Space))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_SPACE.Length;
            }
            if (selection.HasFlag(CharSelection.Brackets))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_BRACKETS.Length;
            }
            if (selection.HasFlag(CharSelection.Other))
            {
                totalCount += SIMPLERANDOMGENERATOR_CHARSELECTION_OTHER.Length;
            }
            return(totalCount);
        }
Пример #2
0
        public static String QuickGetRandomString(
            CharSelection selection,
            Int32 length,
            bool atLeastOneOfEachGroup)
        {
            string        random = String.Empty;
            StringBuilder chars  = new StringBuilder();

            if (selection.HasFlag(CharSelection.Lowercase))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_LOWERCASE);
            }
            if (selection.HasFlag(CharSelection.Uppercase))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_UPPERCASE);
            }
            if (selection.HasFlag(CharSelection.Digits))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_DIGITS);
            }
            if (selection.HasFlag(CharSelection.Minus))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_MINUS);
            }
            if (selection.HasFlag(CharSelection.Underline))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_UNDERLINE);
            }
            if (selection.HasFlag(CharSelection.Space))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_SPACE);
            }
            if (selection.HasFlag(CharSelection.Brackets))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_BRACKETS);
            }
            if (selection.HasFlag(CharSelection.Other))
            {
                chars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_OTHER);
            }
            random = SimpleRandomGenerator.QuickGetRandomString(chars.ToString(), length);

            int groupCount = 0;

            groupCount += (selection.HasFlag(CharSelection.Lowercase)) ? 1 : 0;
            groupCount += (selection.HasFlag(CharSelection.Uppercase)) ? 1 : 0;
            groupCount += (selection.HasFlag(CharSelection.Digits)) ? 1 : 0;
            groupCount += (selection.HasFlag(CharSelection.Minus | CharSelection.Underline | CharSelection.Space | CharSelection.Brackets | CharSelection.Other)) ? 1 : 0;

            if (atLeastOneOfEachGroup && length >= groupCount)
            {
                StringBuilder specialChars = new StringBuilder();
                if (selection.HasFlag(CharSelection.Minus))
                {
                    specialChars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_MINUS);
                }
                if (selection.HasFlag(CharSelection.Underline))
                {
                    specialChars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_UNDERLINE);
                }
                if (selection.HasFlag(CharSelection.Space))
                {
                    specialChars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_SPACE);
                }
                if (selection.HasFlag(CharSelection.Brackets))
                {
                    specialChars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_BRACKETS);
                }
                if (selection.HasFlag(CharSelection.Other))
                {
                    specialChars.Append(SIMPLERANDOMGENERATOR_CHARSELECTION_OTHER);
                }

                bool fixing = true;
                while (fixing)
                {
                    fixing = false;
                    if (selection.HasFlag(CharSelection.Lowercase))
                    {
                        if (!StringContainsOneOf(random, SIMPLERANDOMGENERATOR_CHARSELECTION_LOWERCASE))
                        {
                            fixing = true;
                            string single = SimpleRandomGenerator.QuickGetRandomString(SIMPLERANDOMGENERATOR_CHARSELECTION_LOWERCASE, 1);
                            int    index  = SimpleRandomGenerator.QuickGetRandomInt(1, random.Length) - 1;
                            random = random.Remove(index, 1);
                            random = random.Insert(index, single);
                        }
                    }
                    if (selection.HasFlag(CharSelection.Uppercase))
                    {
                        if (!StringContainsOneOf(random, SIMPLERANDOMGENERATOR_CHARSELECTION_UPPERCASE))
                        {
                            fixing = true;
                            string single = SimpleRandomGenerator.QuickGetRandomString(SIMPLERANDOMGENERATOR_CHARSELECTION_UPPERCASE, 1);
                            int    index  = SimpleRandomGenerator.QuickGetRandomInt(1, random.Length) - 1;
                            random = random.Remove(index, 1);
                            random = random.Insert(index, single);
                        }
                    }
                    if (selection.HasFlag(CharSelection.Digits))
                    {
                        if (!StringContainsOneOf(random, SIMPLERANDOMGENERATOR_CHARSELECTION_DIGITS))
                        {
                            fixing = true;
                            string single = SimpleRandomGenerator.QuickGetRandomString(SIMPLERANDOMGENERATOR_CHARSELECTION_DIGITS, 1);
                            int    index  = SimpleRandomGenerator.QuickGetRandomInt(1, random.Length) - 1;
                            random = random.Remove(index, 1);
                            random = random.Insert(index, single);
                        }
                    }
                    if (specialChars.Length > 0)
                    {
                        if (!StringContainsOneOf(random, specialChars.ToString()))
                        {
                            fixing = true;
                            string single = SimpleRandomGenerator.QuickGetRandomString(specialChars.ToString(), 1);
                            int    index  = SimpleRandomGenerator.QuickGetRandomInt(1, random.Length) - 1;
                            random = random.Remove(index, 1);
                            random = random.Insert(index, single);
                        }
                    }
                }
            }
            return(random);
        }