Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PasswordGenerator : IPasswordGenerator
- {
- private readonly Random _rand = new Random(Environment.TickCount);
- private readonly PasswordOptions _passwordOptions;
- // private readonly ImmutableArray<string> _randomChars = new ImmutableArray<string>
- // {
- // "ABCDEFGHJKLMNOPQRSTUVWXYZ",
- // "abcdefghijkmnopqrstuvwxyz",
- // "0123456789",
- // "!@$?_-"
- // };
- private const string Uppercase = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
- private const string Lowercase = "abcdefghijkmnopqrstuvwxyz";
- private const string Digits = "0123456789";
- private const string Special =
- public PasswordGenerator(PasswordOptions passwordOptions)
- {
- _passwordOptions = passwordOptions;
- if (passwordOptions == null)
- {
- _passwordOptions = new PasswordOptions
- {
- RequiredLength = 8,
- RequiredUniqueChars = 4,
- RequireDigit = true,
- RequireLowercase = true,
- RequireNonAlphanumeric = true,
- RequireUppercase = true
- };
- }
- }
- /// <summary>
- /// Генерирует случайный пароль.
- /// </summary>
- public string Next()
- {
- Span<char> chars = stackalloc char[_passwordOptions.RequiredLength];
- if (_passwordOptions.RequireUppercase)
- {
- chars[_rand.Next(0, chars.Length)] = _randomChars[0][_rand.Next(0, _randomChars[0].Length)];
- }
- if (_passwordOptions.RequireLowercase)
- {
- chars.Insert(_rand.Next(0, chars.Count),
- _randomChars[1][_rand.Next(0, _randomChars[1].Length)]);
- }
- if (_passwordOptions.RequireDigit)
- {
- chars.Insert(_rand.Next(0, chars.Count),
- _randomChars[2][_rand.Next(0, _randomChars[2].Length)]);
- }
- if (_passwordOptions.RequireNonAlphanumeric)
- {
- chars.Insert(_rand.Next(0, chars.Count),
- _randomChars[3][_rand.Next(0, _randomChars[3].Length)]);
- }
- for (int i = chars.Count; i < _passwordOptions.RequiredLength
- || chars.Distinct().Count() < _passwordOptions.RequiredUniqueChars; i++)
- {
- string rcs = _randomChars[_rand.Next(0, _randomChars.Length)];
- chars.Insert(_rand.Next(0, chars.Count),
- rcs[_rand.Next(0, rcs.Length)]);
- }
- return new string(chars.ToArray());
- int DistinctCount(Span<char> charsSpan, int length)
- {
- int uniqueCount;
- Span<char> unique = stackalloc char[length];
- foreach (char elem in charsSpan)
- {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement