/// <summary> /// Generates a password of length <paramref name="passwordLength"/> from the given pool of /// symbols in <paramref name="parameters"/> /// </summary> /// <param name="passwordLength">Length of the password.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> public static string GeneratePassword(int passwordLength, PasswordGeneratorParameters parameters) { #region generate pool of symbols List<char> pool = GenerateSymbolPool(parameters); #endregion generate pool of symbols #region generate password char[] password = new char[passwordLength]; Random rand = new Random(); int poolSize = pool.Count - 1; for (int i = 0; i < passwordLength; i++) { password[i] = pool[rand.Next(0, poolSize)]; } #endregion generate password return new string(password); }
/// <summary> /// Generates a pool of symbols /// </summary> /// <param name="parameters">The parameters.</param> /// <returns></returns> private static List<char> GenerateSymbolPool(PasswordGeneratorParameters parameters) { List<char> pool = new List<char>(); // copy the alpha chars into the source pool if (parameters.HasFlag(PasswordGeneratorParameters.AlphaLowercase)) { pool.AddRange(alpha); } // add the alpha capital to the source pool if (parameters.HasFlag(PasswordGeneratorParameters.AlphaCapital)) { pool.AddRange(alphaCaps); } // add the numeric values to the source pool if (parameters.HasFlag(PasswordGeneratorParameters.Numeric)) { pool.AddRange(numeric); } // add the symbols to the source pool if (parameters.HasFlag(PasswordGeneratorParameters.Symbols)) { pool.AddRange(symbols); } return pool; }