Ejemplo n.º 1
0
        bool RequirementsAreValid(PasswordRequirements requirements, out string reason)
        {
            reason = "";

            //check requirements object is not null
            if (requirements is null)
            {
                reason = "Password requirements are null.";
                return(false);
            }

            //check valid min length
            if (requirements.MinLength > requirements.MaxLength)
            {
                reason = "MinLength cannot be greater than MaxLength.";
                return(false);
            }

            //check other parameters are not exceeding max length
            if ((requirements.MinLowerAlphaChars +
                 requirements.MinNumericChars +
                 requirements.MinSpecialChars +
                 requirements.MinUpperAlphaChars)
                > requirements.MaxLength)
            {
                reason = "Combination of MinLowerAlphaChars, MinNumericChars, MinSpecialChars and MinUpperAlphaChars cannot be greater than MaxLength.";
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public string GeneratePassword(PasswordRequirements requirements)
        {
            //recieves any validation errors
            string error;

            if (RequirementsAreValid(requirements, out error))
            {
                int           passwordLength  = requirements.MaxLength;
                StringBuilder passwordBuilder = new StringBuilder();

                //get requested numeric characters
                if (requirements.MinNumericChars > 0)
                {
                    passwordBuilder.Append(GetNumericChars(requirements.MinNumericChars));
                    passwordLength -= requirements.MinNumericChars;
                }

                //get requested special characters
                if (requirements.MinSpecialChars > 0)
                {
                    passwordBuilder.Append(GetSpecialChars(requirements.MinSpecialChars));
                    passwordLength -= requirements.MinSpecialChars;
                }

                //get requested alpha characters in uppercase
                if (requirements.MinUpperAlphaChars > 0)
                {
                    passwordBuilder.Append(GetUpperChars(requirements.MinUpperAlphaChars));
                    passwordLength -= requirements.MinUpperAlphaChars;
                }

                //get requested alpha characters in lowercase
                if (requirements.MinLowerAlphaChars > 0)
                {
                    passwordBuilder.Append(GetLowerChars(requirements.MinLowerAlphaChars));
                    passwordLength -= requirements.MinLowerAlphaChars;
                }

                //get random remaining characters
                if (passwordLength > 0)
                {
                    while (passwordLength > 0)
                    {
                        GetCharDelegate[] getCharDelegate = { GetLowerChars, GetUpperChars, GetSpecialChars, GetNumericChars };

                        passwordBuilder.Append(getCharDelegate[rnd.Next(getCharDelegate.Length)](1));
                        passwordLength--;
                    }
                }
                return(passwordBuilder.ToString().Shuffle());
            }

            throw new Exception(string.IsNullOrEmpty(error) ? "Something went wrong!!" : error);
        }
Ejemplo n.º 3
0
        public string GeneratePassword(PasswordRequirements requirements)
        {
            if (requirements == null)
            {
                throw new ArgumentNullException(nameof(requirements));
            }

            this.requirements = requirements;

            GenerateRequiredChars();
            GenerateMissingChars();

            var password = string.Join("", passwordChars);

            return(password);
        }
Ejemplo n.º 4
0
        public string GeneratePassword(PasswordRequirements requirements)
        {
            StringBuilder stringBuilder  = new StringBuilder(new String(' ', random.Next(requirements.MinLength, requirements.MaxLength)));
            int           stringIterator = 0;

            // Add lowercase chars
            for (int i = 0; i < requirements.MinLowerAlphaChars; i++)
            {
                stringBuilder[stringIterator] = GenerateCharFromString(lowercaseChars);
                stringIterator++;
            }

            // Add uppercase chars
            for (int i = 0; i < requirements.MinUpperAlphaChars; i++)
            {
                stringBuilder[stringIterator] = GenerateCharFromString(uppercaseChars);
                stringIterator++;
            }

            // Add numeric chars
            for (int i = 0; i < requirements.MinNumericChars; i++)
            {
                stringBuilder[stringIterator] = GenerateCharFromString(numbericChars);
                stringIterator++;
            }

            // Add symbols
            for (int i = 0; i < requirements.MinSpecialChars; i++)
            {
                stringBuilder[stringIterator] = GenerateCharFromString(symbolChars);
                stringIterator++;
            }

            // Fill in missing chars
            for (int i = stringIterator; i < stringBuilder.Length; i++)
            {
                stringBuilder[stringIterator] = GenerateCharFromString(symbolChars + numbericChars + uppercaseChars + lowercaseChars);
                stringIterator++;
            }

            string shuffledString = ShuffleString(stringBuilder.ToString());

            return(shuffledString);
        }