passwdgen/passwdgen.cpp

36 lines
1.2 KiB
C++
Raw Normal View History

2025-09-05 11:43:46 +00:00
// passwdgen.cpp
#include "passwdgen.h"
2020-10-22 11:13:08 +00:00
#include <iostream>
#include <random>
std::string random_string(std::size_t length, bool punc) {
std::string CHARACTERS;
std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2025-09-05 10:59:22 +00:00
const std::string SPECIALS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
2020-10-22 11:13:08 +00:00
if (punc) {
CHARACTERS = ALPHANUM.append(SPECIALS);
} else {
CHARACTERS = ALPHANUM;
}
2025-09-05 10:59:22 +00:00
2020-10-22 11:13:08 +00:00
std::random_device random_device;
std::mt19937 generator(random_device());
2025-09-05 10:59:22 +00:00
std::uniform_int_distribution<> distribution(0, static_cast<int>(CHARACTERS.size() - 1));
2020-10-22 11:13:08 +00:00
std::string random_string;
for (std::size_t i = 0; i < length; ++i) {
random_string += CHARACTERS[distribution(generator)];
}
return random_string;
}
2025-09-05 10:59:22 +00:00
void show_usage(const std::string& name) {
std::cerr << "Usage: " << name << " [OPTIONS] " << std::endl
2020-10-22 11:13:08 +00:00
<< "Options:" << std::endl
<< "\t-h, --help \t\tShow this help message" << std::endl
<< "\t-l, --length [n] \t\tThe length of the password (default: 32)" << std::endl
2020-10-22 11:13:08 +00:00
<< "\t-p, --punctuation \t\tToggle special characters (default: false)"
<< std::endl;
}