passwdgen/src/passwdgen.cpp
Gregory Gauthier 24adb7d618
All checks were successful
CMake / build (push) Successful in 42s
refactor(cli): improve argument parsing and update special characters
- Enhance CLI handling in main.cpp with better error messages, default length as size_t, and proper option validation
- Simplify special characters in passwdgen.cpp to a safer, common subset
2026-03-06 16:52:59 +00:00

36 lines
1.2 KiB
C++

// passwdgen.cpp
#include "../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;
}