Changed args to support minimum amounts

This commit is contained in:
Bob Polis 2022-06-11 22:50:54 +02:00
parent d7b44afe11
commit 4629c59b45

View File

@ -2,81 +2,89 @@
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <climits>
#include <regex>
#include <algorithm>
#include <getopt.h>
#include <libscnumerics.hpp>
void print_help() {
std::cout << "usage: pw [-h|--version] [-c <len>] [-d] [-l] [-u] [-s]\n";
std::cout << " -h, --help show this help text and exit\n";
std::cout << " --version show version number and exit\n";
std::cout << " -c, --count <num> password length; default is 24 characters\n";
std::cout << " -d, --digit allow digits\n";
std::cout << " -l, --lower allow lower case letters\n";
std::cout << " -u, --upper allow upper case letters\n";
std::cout << " -s, --symbol allow symbols and punctuation\n";
std::cout << " -S, --special allow symbols from specified argument\n";
std::cout << "When none of -d, -l, -u, and -s are given, pw acts as if all had been specified.\n";
}
// globals
std::string upper {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
std::string lower {"abcdefghijklmnopqrstuvwxyz"};
std::string digits {"0123456789"};
std::string symbols {"_-=+<>,.!@#$%^&*"};
std::string special;
std::string valid;
int len {24};
int min_upper {0};
int min_lower {0};
int min_digits {0};
int min_symbols {0};
int min_special {0};
void print_version() {
std::cout << "pw version 1.0\n";
void validate_range(const std::string& arg, int& min_val, int& max_val) {
std::regex pat {R"((\d+)(?:-(\d+))?)"};
std::smatch match;
if (std::regex_match(arg, match, pat)) {
min_val = std::atoi(match[1].str().c_str());
if (match.size() > 2) {
max_val = std::atoi(match[2].str().c_str());
}
}
throw std::runtime_error("invalid range");
}
int main(int argc, char* argv[]) {
try {
std::string upper {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
std::string lower {"abcdefghijklmnopqrstuvwxyz"};
std::string digits {"0123456789"};
std::string symbols {"_-=+<>,.!@#$%^&*"};
std::string valid;
int len {24};
bool noflags {true};
int opt_char, opt_val;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, &opt_val, 1},
{"upper", no_argument, nullptr, 'u'},
{"lower", no_argument, nullptr, 'l'},
{"digit", no_argument, nullptr, 'd'},
{"symbol", no_argument, nullptr, 's'},
{"special", required_argument, nullptr, 'S'},
{"count", required_argument, nullptr, 'c'},
{"digit", required_argument, nullptr, 'd'},
{"lower", required_argument, nullptr, 'l'},
{"symbol", required_argument, nullptr, 's'},
{"special", required_argument, nullptr, 'S'},
{"upper", required_argument, nullptr, 'u'},
{nullptr, 0, nullptr, 0}
};
while ((opt_char = getopt_long(argc, argv, "c:huldsS:", long_options, nullptr)) != -1) {
while ((opt_char = getopt_long(argc, argv, "c:u:l:d:s:S:", long_options, nullptr)) != -1) {
std::string arg {optarg ? optarg : ""};
switch (opt_char) {
case 0: {
// handle long-only options here
switch (opt_val) {
case 1:
print_version();
std::cout << "pw version 2.0, © 2022 Bob Polis. All rights reserved.\n";
return EXIT_SUCCESS;
}
break;
}
case 'h':
print_help();
return EXIT_SUCCESS;
case 'u':
noflags = false;
valid += upper;
min_upper = std::atoi(optarg);
noflags = false;
break;
case 'l':
noflags = false;
valid += lower;
min_lower = std::atoi(optarg);
noflags = false;
break;
case 'd':
noflags = false;
valid += digits;
min_digits = std::atoi(optarg);
noflags = false;
break;
case 's':
noflags = false;
valid += symbols;
min_symbols = std::atoi(optarg);
noflags = false;
break;
case 'S':
noflags = false;
special = arg;
valid += arg;
min_special = 1;
noflags = false;
break;
case 'c':
len = std::stoi(optarg);
@ -85,23 +93,34 @@ int main(int argc, char* argv[]) {
throw std::runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
if (noflags) {
valid = upper + lower + digits + symbols;
}
for (int i = 0; i < len; ++i) {
std::cout << sc::random::choice<char>(valid.begin(), valid.end());
int num_free_choice = len - min_digits - min_lower - min_upper - min_symbols;
if (num_free_choice < 0) {
throw std::runtime_error("total minimum required exceeds password length");
}
std::cout << std::endl;
std::string password;
for (int i = 0; i < min_digits; ++i) {
password += sc::random::choice<char>(digits.begin(), digits.end());
}
for (int i = optind; i < argc; ++i) {
try {
// process file argv[i]
} catch (const std::runtime_error& ex) {
std::cerr << "pw: " << ex.what() << '\n';
for (int i = 0; i < min_lower; ++i) {
password += sc::random::choice<char>(lower.begin(), lower.end());
}
for (int i = 0; i < min_upper; ++i) {
password += sc::random::choice<char>(upper.begin(), upper.end());
}
for (int i = 0; i < min_symbols; ++i) {
password += sc::random::choice<char>(symbols.begin(), symbols.end());
}
if (min_special) {
password += sc::random::choice<char>(special.begin(), special.end());
}
for (int i = 0; i < num_free_choice; ++i) {
password += sc::random::choice<char>(valid.begin(), valid.end());
}
std::shuffle(password.begin(), password.end(), sc::random::instance().engine());
std::cout << password << std::endl;
} catch (const std::exception& ex) {
std::cerr << "pw: " << ex.what() << '\n';
return EXIT_FAILURE;