Last active
August 20, 2021 17:38
-
-
Save saulozitos/af3a78674d34b416e34b0368193663a8 to your computer and use it in GitHub Desktop.
An example of a secure password generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <algorithm> | |
| #include <random> | |
| #include <string> | |
| class PasswordGenerator | |
| { | |
| public: | |
| PasswordGenerator() = default; | |
| std::string newPassword(int size, bool hard = false) | |
| { | |
| if(size < 10){ | |
| size = 10; | |
| } | |
| if(hard && size > 90){ | |
| size = 90; | |
| } | |
| static std::uniform_int_distribution<> distribution(33, 126); | |
| static std::default_random_engine generator; | |
| std::string pass; | |
| for (ushort it = 0; it < size; ++it) { | |
| auto num = distribution(generator); | |
| if(hard){ | |
| while (pass.find(static_cast<char>(num)) != std::string::npos) | |
| num = distribution(generator); | |
| } | |
| pass += static_cast<char>(num); | |
| } | |
| return pass; | |
| } | |
| }; | |
| int main() | |
| { | |
| PasswordGenerator pg; | |
| std::cout << pg.newPassword(25) << "\n"; | |
| std::cout << pg.newPassword(25, true) << "\n"; | |
| std::cout << pg.newPassword(25, true) << "\n"; | |
| std::cout << pg.newPassword(25) << "\n"; | |
| std::cout << pg.newPassword(90) << "\n"; | |
| std::cout << pg.newPassword(90) << "\n"; | |
| std::cout << pg.newPassword(90) << "\n"; | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://godbolt.org/z/fPYezxP6f