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