PasswordGenerator.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace LoginGenerator.Models
  7. {
  8. public class PasswordGenerator
  9. {
  10. Random random = new Random();
  11. int[,] lines =
  12. {
  13. { 48, 58 },
  14. { 65, 91 },
  15. { 97, 123 }
  16. };
  17. public string Execute()
  18. {
  19. if (TryGenerate(out string password)) return password;
  20. else return Execute();
  21. }
  22. bool TryGenerate(out string password)
  23. {
  24. password = "";
  25. int[] passwordLines = new int[8];
  26. for (int i = 0; i < 8; i++)
  27. {
  28. passwordLines[i] = random.Next(0, 3);
  29. password += Convert.ToChar(random.Next(lines[passwordLines[i], 0], lines[passwordLines[i], 1])).ToString();
  30. }
  31. if (passwordLines.Contains(0) && passwordLines.Contains(1) && passwordLines.Contains(2)) return true;
  32. else return false;
  33. }
  34. }
  35. }