Int32Between() public static method

Generates a cryptographically strong 32-bit random integer between specified values. i.e. [startNumber-stopNumber)
The cryptographic service provider (CSP) cannot be acquired.
public static Int32Between ( int startNumber, int stopNumber ) : int
startNumber int A that is the low end of our range.
stopNumber int A that is the high end of our range.
return int
Ejemplo n.º 1
0
        /// <summary>
        /// Generates a password with the given length.
        /// </summary>
        /// <param name="length">The length of the password to be generated.</param>
        /// <returns>A randomly generated password.</returns>
        public string GeneratePassword(int length)
        {
            List <CharacterGroup> characterGroups;

            char[] passwordChars;

            if (length < MinLength)
            {
                throw new ArgumentOutOfRangeException(nameof(length), $"The length of the generated password must be at least {MinLength} characters.");
            }

            // Create a collection of character groups the size of the password to be generated.
            // The number of times a character group appears in this collection is equal to the
            // minimum number of occurrences of that character group. An AllCharacters group is
            // created to fill in the characters not accounted for by the other character groups.
            characterGroups = m_characterGroups
                              .SelectMany(characterGroup => Enumerable.Repeat(characterGroup, characterGroup.MinOccurrence))
                              .Concat(Enumerable.Repeat(new CharacterGroup(AllCharacters), length - MinLength))
                              .ToList();

            passwordChars = new char[length];

            for (int i = 0; i < length; i++)
            {
                // Pull a random character from the character group into the password
                CharacterGroup characterGroup = characterGroups[i];
                int            index          = Random.Int32Between(0, characterGroup.Characters.Length);
                passwordChars[i] = characterGroup.Characters[index];
            }

            // Scramble the generated set of characters
            passwordChars.Scramble();

            return(new string(passwordChars));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a password with length between the given
        /// <paramref name="minLength"/> and <paramref name="maxLength"/>.
        /// </summary>
        /// <param name="minLength">The minimum length of the generated password.</param>
        /// <param name="maxLength">The maximum length of the generated password.</param>
        /// <returns>A randomly generated password.</returns>
        public string GeneratePassword(int minLength, int maxLength)
        {
            if (minLength > maxLength)
            {
                throw new ArgumentException("minLength must be less than or equal to maxLength");
            }

            return(GeneratePassword(Random.Int32Between(minLength, maxLength + 1)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a random password of the specified <paramref name="length"/> with at least one uppercase letter, one lowercase letter, one special character and one digit.
        /// </summary>
        /// <param name="length">Length of the password to generate.</param>
        /// <returns>Randomly generated password of the specified <paramref name="length"/>.</returns>
        /// <exception cref="ArgumentException">A value of less than 8 is specified for the <paramref name="length"/>.</exception>
        public static string GeneratePassword(int length)
        {
            if (length < 8)
            {
                throw new ArgumentException("Value must be at least 8", "length");
            }

            // ASCII character ranges:
            // Lower case - 97 to 122
            // Upper case - 65 to 90
            // Special character - 33 to 47
            // Digits - 48 to 57

            int cursor  = 0;
            int lower   = Random.Int32Between(1, length / 2);
            int upper   = Random.Int32Between(1, (length - lower) / 2);
            int special = Random.Int32Between(1, (length - (lower + upper)) / 2);
            int digits  = length - (lower + upper + special);

            char[] password = new char[length];
            for (int i = 0; i < lower; i++)
            {
                password[cursor] = (char)Random.Int32Between(97, 122);
                cursor++;
            }
            for (int i = 0; i < upper; i++)
            {
                password[cursor] = (char)Random.Int32Between(65, 90);
                cursor++;
            }
            for (int i = 0; i < special; i++)
            {
                password[cursor] = (char)Random.Int32Between(33, 47);
                cursor++;
            }
            for (int i = 0; i < digits; i++)
            {
                password[cursor] = (char)Random.Int32Between(48, 57);
                cursor++;
            }

            // Scramble for more randomness.
            List <char> scrambledPassword = new List <char>(password);

            scrambledPassword.Scramble();

            return(new string(scrambledPassword.ToArray()));
        }