36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
// passwdgen.cpp
|
|
#include "passwdgen.h"
|
|
#include <iostream>
|
|
#include <random>
|
|
|
|
std::string random_string(std::size_t length, bool punc) {
|
|
std::string CHARACTERS;
|
|
std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
const std::string SPECIALS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
|
|
if (punc) {
|
|
CHARACTERS = ALPHANUM.append(SPECIALS);
|
|
} else {
|
|
CHARACTERS = ALPHANUM;
|
|
}
|
|
|
|
std::random_device random_device;
|
|
std::mt19937 generator(random_device());
|
|
std::uniform_int_distribution<> distribution(0, static_cast<int>(CHARACTERS.size() - 1));
|
|
|
|
std::string random_string;
|
|
for (std::size_t i = 0; i < length; ++i) {
|
|
random_string += CHARACTERS[distribution(generator)];
|
|
}
|
|
return random_string;
|
|
}
|
|
|
|
void show_usage(const std::string& name) {
|
|
std::cerr << "Usage: " << name << " [OPTIONS] " << std::endl
|
|
<< "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
|
|
<< "\t-p, --punctuation \t\tToggle special characters (default: false)"
|
|
<< std::endl;
|
|
}
|