QwarkDev

Password Generator | C++

Jan 4th, 2021 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. // author - aws (nt_qwark)
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5.  
  6. struct PasswordGenerateSettings
  7. {
  8. public:
  9.     bool UseUpper = true;
  10.     bool UseDigits = true;
  11.     bool OnlyUpper = false;
  12.     bool OnlyDigits = false;
  13.     char DelemiterSymbol = '-';
  14.     unsigned SectionsCount = 4;
  15.     unsigned SymbolsInSection = 5;
  16. };
  17.  
  18. std::string GeneratePassword(PasswordGenerateSettings& settings)
  19. {
  20.     std::stringstream buffer;
  21.  
  22.     for (size_t i = 0; i < static_cast<size_t>(settings.SectionsCount * settings.SymbolsInSection); i++)
  23.     {
  24.         char randChar = rand() % 25 + 'a';
  25.         bool isUpper = rand() % 2;
  26.         bool isDigit = rand() % 2;
  27.  
  28.         isUpper = isUpper || settings.OnlyUpper;
  29.         isDigit = isDigit || settings.OnlyDigits;
  30.  
  31.         if (settings.UseUpper && isUpper)
  32.             randChar = toupper(randChar);
  33.  
  34.         if (isDigit)
  35.             randChar = rand() % 10 + '0';
  36.  
  37.         if (i % settings.SymbolsInSection == 0 && i != 0)
  38.             buffer << settings.DelemiterSymbol;
  39.        
  40.         buffer << randChar;
  41.     }
  42.  
  43.     return buffer.str();
  44. }
  45.  
  46. // generate pass with default settings
  47. std::string GeneratePassword()
  48. {
  49.     PasswordGenerateSettings defaultSettings;
  50.     return GeneratePassword(defaultSettings);
  51. }
  52.  
  53. int main()
  54. {
  55.     srand(clock());
  56.  
  57.     while (true)
  58.     std::cout << GeneratePassword() << std::endl;
  59.    
  60.     return (0);
  61. }
  62.  
  63. // example output:
  64. // 40J6o-7J0o1-981e0-r54dE
  65. // 1Yn96-x6b4e-4m574-570xE
  66. // etc
Add Comment
Please, Sign In to add comment