コード例 #1
0
        /// <summary>
        ///     Common method to generate the password for strings and <see cref="SecureString"/>s
        /// </summary>
        /// <typeparam name="T">The type of the password to return</typeparam>
        /// <param name="length">The length of the password to generate</param>
        /// <param name="allowedCharacters">Set of allowed characters in the generated password</param>
        /// <param name="excludeCharacters">Set of disallowed characters in the generated password</param>
        /// <param name="initialValue">Function to generate the initial password to return</param>
        /// <param name="appender">Function to append a character to the password</param>
        /// <returns>The generated password</returns>
        private static T InternalGenerate <T>(int length, PasswordCharacters allowedCharacters,
                                              IEnumerable <char> excludeCharacters, Func <T> initialValue, Action <T, char, int> appender)
        {
            if (length <= 0)
            {
                throw new ArgumentOutOfRangeException("length", "Password length must be greater than zero");
            }

            // Create a byte array the same length as the expected password and populate it with
            // random bytes
            var randomBytes           = new byte[length];
            var randomNumberGenerator = new RNGCryptoServiceProvider();

            randomNumberGenerator.GetBytes(randomBytes);

            // Create a string of all the characters allowed in the password
            string allowedCharactersString = GenerateAllowedCharactersString(allowedCharacters, excludeCharacters);
            int    allowedCharactersCount  = allowedCharactersString.Length;

            // Create the password
            T password = initialValue();

            for (int i = 0; i < length; i++)
            {
                appender(password, allowedCharactersString[randomBytes[i] % allowedCharactersCount], i);
            }
            return(password);
        }
コード例 #2
0
 /// <summary>
 ///     Generates a random password based on the provided criteria.
 /// </summary>
 /// <param name="length">The length of the password to generate</param>
 /// <param name="allowedCharacters">Set of allowed characters in the generated password</param>
 /// <param name="excludeCharacters">Set of disallowed characters in the generated password</param>
 /// <returns>The generated password</returns>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 public static string Generate(int length, PasswordCharacters allowedCharacters,
                               IEnumerable <char> excludeCharacters)
 {
     char[] password = InternalGenerate(length, allowedCharacters, excludeCharacters, () => new char[length],
                                        (pw, ch, index) => pw[index] = ch);
     return(new string(password));
 }
コード例 #3
0
            public static SecureString GenerateSecure(int length, PasswordCharacters allowedCharacters = PasswordCharacters.All,
                                                      IEnumerable <char> excludedCharacters            = null)
            {
                if (length <= 0)
                {
                    throw new ArgumentOutOfRangeException("length", "Password length must be greater than zero");
                }

                var randomBytes           = new byte[length];
                var randomNumberGenerator = new RNGCryptoServiceProvider();

                randomNumberGenerator.GetBytes(randomBytes);

                string allowedCharactersString = GenerateAllowedCharactersString(allowedCharacters, excludedCharacters);
                int    allowedCharactersCount  = allowedCharactersString.Length;

                var password = new SecureString();

                for (int i = 0; i < length; i++)
                {
                    password.AppendChar(allowedCharactersString[randomBytes[i] % allowedCharactersCount]);
                }
                password.MakeReadOnly();
                return(password);
            }
コード例 #4
0
        /// <summary>
        ///     Generates a random password based on the provided criteria and returns it as a
        ///     <see cref="SecureString" />.
        /// </summary>
        /// <param name="length">The length of the password to generate</param>
        /// <param name="allowedCharacters">Set of allowed characters in the generated password</param>
        /// <param name="excludeCharacters">Set of disallowed characters in the generated password</param>
        /// <returns>The generated password as a <see cref="SecureString" /></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static SecureString GenerateSecure(int length, PasswordCharacters allowedCharacters,
                                                  IEnumerable <char> excludeCharacters)
        {
            SecureString password = InternalGenerate(length, allowedCharacters, excludeCharacters,
                                                     () => new SecureString(),
                                                     (pw, ch, index) => pw.AppendChar(ch));

            password.MakeReadOnly();
            return(password);
        }
コード例 #5
0
 public PasswordGenerator(
     int length,
     PasswordCharacters includedCharacters = PasswordCharacters.All,
     char[] excludedCharacters             = null)
 {
     Length                 = length;
     IncludedCharacters     = includedCharacters;
     ExcludedCharacters     = excludedCharacters;
     RequiresStrongPassword = true;
 }
コード例 #6
0
        public void Excluded_characters_are_excluded(PasswordCharacters allowedCharacters, string excludeCharacters)
        {
            for (int i = 0; i < 50; i++)
            {
                string password = PasswordGenerator.Generate(40, allowedCharacters, excludeCharacters);
                foreach (char excludeChar in excludeCharacters)
                {
                    password.ShouldNotContain(excludeChar);
                }

                SecureString securePassword = PasswordGenerator.GenerateSecure(40, allowedCharacters, excludeCharacters);
                foreach (char excludeChar in excludeCharacters)
                {
                    SecureStringToString(securePassword).ShouldNotContain(excludeChar);
                }
            }
        }
コード例 #7
0
        private static string GenerateAllowedCharactersString(PasswordCharacters characters, IEnumerable <char> excludeCharacters)
        {
            var allowedCharactersString = new StringBuilder();

            foreach (var type in AllowedPasswordCharacters.Where(type => (characters & type.Key) == type.Key))
            {
                if (excludeCharacters == null)
                {
                    allowedCharactersString.Append(type.Value);
                }
                else
                {
                    allowedCharactersString.Append(type.Value.Where(c => !excludeCharacters.Contains(c)).ToArray());
                }
            }
            return(allowedCharactersString.ToString());
        }
コード例 #8
0
ファイル: PasswordGenerator.cs プロジェクト: lycilph/Projects
        public static string Generate(int length, PasswordCharacters allowedCharacters = PasswordCharacters.All,
            IEnumerable<char> excludeCharacters = null)
        {
            if (length <= 0)
                throw new ArgumentOutOfRangeException("length", "Password length must be greater than zero");

            var randomBytes = new byte[length];
            var randomNumberGenerator = new RNGCryptoServiceProvider();
            randomNumberGenerator.GetBytes(randomBytes);

            string allowedCharactersString = GenerateAllowedCharactersString(allowedCharacters, excludeCharacters);
            int allowedCharactersCount = allowedCharactersString.Length;

            var characters = new char[length];
            for (int i = 0; i < length; i++)
                characters[i] = allowedCharactersString[randomBytes[i] % allowedCharactersCount];
            return new string(characters);
        }
コード例 #9
0
        private static char[] GenerateCharacters(PasswordCharacters includedCharacters, IEnumerable <char> excludedCharacters)
        {
            var charactersBuilder = new StringBuilder();

            foreach (var character in Characters.Where(character => (character.Key & includedCharacters) == character.Key))
            {
                if (excludedCharacters != null)
                {
                    charactersBuilder.Append(character.Value.Where(c => !excludedCharacters.Contains(c)).ToArray());
                }
                else
                {
                    charactersBuilder.Append(character.Value);
                }
            }

            return(charactersBuilder.ToString().ToCharArray());
        }
コード例 #10
0
ファイル: PasswordGenerator.cs プロジェクト: lycilph/Projects
        public static SecureString GenerateSecure(int length, PasswordCharacters allowedCharacters = PasswordCharacters.All,
            IEnumerable<char> excludedCharacters = null)
        {
            if (length <= 0)
                throw new ArgumentOutOfRangeException("length", "Password length must be greater than zero");

            var randomBytes = new byte[length];
            var randomNumberGenerator = new RNGCryptoServiceProvider();
            randomNumberGenerator.GetBytes(randomBytes);

            string allowedCharactersString = GenerateAllowedCharactersString(allowedCharacters, excludedCharacters);
            int allowedCharactersCount = allowedCharactersString.Length;

            var password = new SecureString();
            for (int i = 0; i < length; i++)
                password.AppendChar(allowedCharactersString[randomBytes[i] % allowedCharactersCount]);
            password.MakeReadOnly();
            return password;
        }
コード例 #11
0
        public static string Generate(int length, PasswordCharacters includedCharacters = PasswordCharacters.All, IEnumerable <char> excludedCharacters = null)
        {
            if (length <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), "The password length must be greater than zero.");
            }

            var randomCharacters      = new char[length];
            var characters            = GenerateCharacters(includedCharacters, excludedCharacters);
            var charactersLength      = characters.Length;
            var randomBytes           = new byte[length];
            var randomNumberGenerator = new RNGCryptoServiceProvider();

            randomNumberGenerator.GetBytes(randomBytes);

            for (var index = 0; index < length; index++)
            {
                randomCharacters[index] = characters[randomBytes[index] % charactersLength];
            }

            return(new string(randomCharacters));
        }
コード例 #12
0
            public static string Generate(int length, PasswordCharacters allowedCharacters = PasswordCharacters.All, IEnumerable <char> excludeCharacters = null)
            {
                if (length <= 0)
                {
                    throw new ArgumentOutOfRangeException("length", "Password length must be greater than zero");
                }

                var randomBytes           = new byte[length];
                var randomNumberGenerator = new RNGCryptoServiceProvider();

                randomNumberGenerator.GetBytes(randomBytes);

                string allowedCharactersString = GenerateAllowedCharactersString(allowedCharacters, excludeCharacters);
                int    allowedCharactersCount  = allowedCharactersString.Length;

                var characters = new char[length];

                for (int i = 0; i < length; i++)
                {
                    characters[i] = allowedCharactersString[randomBytes[i] % allowedCharactersCount];
                }
                return(new string(characters));
            }
コード例 #13
0
ファイル: PasswordGenerator.cs プロジェクト: lycilph/Projects
 private static string GenerateAllowedCharactersString(PasswordCharacters characters, IEnumerable<char> excludeCharacters)
 {
     var allowedCharactersString = new StringBuilder();
     foreach (KeyValuePair<PasswordCharacters, string> type in AllowedPasswordCharacters)
     {
         if ((characters & type.Key) != type.Key)
             continue;
         if (excludeCharacters == null)
             allowedCharactersString.Append(type.Value);
         else
             allowedCharactersString.Append(type.Value.Where(c => !excludeCharacters.Contains(c)).ToArray());
     }
     return allowedCharactersString.ToString();
 }
コード例 #14
0
 /// <summary>
 ///     Generates a random password based on the provided criteria and returns it as a
 ///     <see cref="SecureString" />.
 /// </summary>
 /// <param name="length">The length of the password to generate</param>
 /// <param name="allowedCharacters">Set of allowed characters in the generated password</param>
 /// <returns>The generated password as a <see cref="SecureString" /></returns>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 public static SecureString GenerateSecure(int length, PasswordCharacters allowedCharacters)
 {
     return(GenerateSecure(length, allowedCharacters, null));
 }
コード例 #15
0
 public string GeneratePassword(int length, PasswordCharacters allowedCharacters)
 {
     return(Password = PasswordGenerator.Generate(length, allowedCharacters));
 }