Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // author - aws (nt_qwark)
- #include <iostream>
- #include <sstream>
- struct PasswordGenerateSettings
- {
- public:
- bool UseUpper = true;
- bool UseDigits = true;
- bool OnlyUpper = false;
- bool OnlyDigits = false;
- char DelemiterSymbol = '-';
- unsigned SectionsCount = 4;
- unsigned SymbolsInSection = 5;
- };
- std::string GeneratePassword(PasswordGenerateSettings& settings)
- {
- std::stringstream buffer;
- for (size_t i = 0; i < static_cast<size_t>(settings.SectionsCount * settings.SymbolsInSection); i++)
- {
- char randChar = rand() % 25 + 'a';
- bool isUpper = rand() % 2;
- bool isDigit = rand() % 2;
- isUpper = isUpper || settings.OnlyUpper;
- isDigit = isDigit || settings.OnlyDigits;
- if (settings.UseUpper && isUpper)
- randChar = toupper(randChar);
- if (isDigit)
- randChar = rand() % 10 + '0';
- if (i % settings.SymbolsInSection == 0 && i != 0)
- buffer << settings.DelemiterSymbol;
- buffer << randChar;
- }
- return buffer.str();
- }
- // generate pass with default settings
- std::string GeneratePassword()
- {
- PasswordGenerateSettings defaultSettings;
- return GeneratePassword(defaultSettings);
- }
- int main()
- {
- srand(clock());
- while (true)
- std::cout << GeneratePassword() << std::endl;
- return (0);
- }
- // example output:
- // 40J6o-7J0o1-981e0-r54dE
- // 1Yn96-x6b4e-4m574-570xE
- // etc
Add Comment
Please, Sign In to add comment