Skip to content

Instantly share code, notes, and snippets.

@saulozitos
Last active August 20, 2021 17:38
Show Gist options
  • Select an option

  • Save saulozitos/af3a78674d34b416e34b0368193663a8 to your computer and use it in GitHub Desktop.

Select an option

Save saulozitos/af3a78674d34b416e34b0368193663a8 to your computer and use it in GitHub Desktop.
An example of a secure password generator
#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;
}
@saulozitos
Copy link
Author

saulozitos commented Aug 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment