Advertisement
ZergRushA

C++ password generator

Jul 24th, 2023 (edited)
1,790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4.  
  5. const std::string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6. const std::string lowerCase = "abcdefghijklmnopqrstuvwxyz";
  7. const std::string digits = "0123456789";
  8. const std::string special = "!\"#$%&'*+,./:;=?@\\^`|~";
  9.  
  10. std::random_device dev;
  11. std::mt19937 rng(dev());
  12.  
  13. std::uniform_int_distribution<std::mt19937::result_type> dist4(1, 4);
  14. std::uniform_int_distribution<std::mt19937::result_type> distUC(0, upperCase.length()-1);
  15. std::uniform_int_distribution<std::mt19937::result_type> distLC(0, lowerCase.length()-1);
  16. std::uniform_int_distribution<std::mt19937::result_type> distDig(0, digits.length()-1);
  17. std::uniform_int_distribution<std::mt19937::result_type> distSpec(0, special.length()-1);
  18.  
  19.  
  20. std::string generate(int len){
  21.     std::string result{};
  22.     int group{};
  23.     int index{};
  24.     for(size_t i = 0; i < len; ++i){
  25.         group = dist4(rng);
  26.        
  27.         switch(group){
  28.            
  29.             case 1:
  30.                 index = distUC(rng);
  31.                 result += upperCase[index];
  32.                 break;
  33.            
  34.             case 2:
  35.                 index = distLC(rng);
  36.                 result += lowerCase[index];
  37.                 break;
  38.            
  39.             case 3:
  40.                 index = distDig(rng);
  41.                 result += digits[index];
  42.                 break;
  43.            
  44.             case 4:
  45.                 index = distSpec(rng);
  46.                 result += special[index];
  47.                 break;
  48.         }
  49.     }
  50.    
  51.     return result;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.  
  58.     int ln = 10;
  59.     for(int i = 0; i < 5; ++i){
  60.         std::cout << generate(ln) << '\n';
  61.     }
  62.    
  63.  
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement