Change indent to 2 spaces
This commit is contained in:
234
src/main.cpp
234
src/main.cpp
@@ -1,121 +1,131 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <climits>
|
||||
#include <algorithm>
|
||||
#include <getopt.h>
|
||||
#include <libscnumerics.hpp>
|
||||
#include "version.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <getopt.h>
|
||||
#include <iostream>
|
||||
#include <libscnumerics.hpp>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// globals
|
||||
std::string upper {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
||||
std::string lower {"abcdefghijklmnopqrstuvwxyz"};
|
||||
std::string digits {"0123456789"};
|
||||
std::string symbols {"_-=+<>,.!@#$%^&*"};
|
||||
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};
|
||||
int len{24};
|
||||
int min_upper{0};
|
||||
int min_lower{0};
|
||||
int min_digits{0};
|
||||
int min_symbols{0};
|
||||
int min_special{0};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
bool noflags {true};
|
||||
int opt_char, opt_val;
|
||||
struct option long_options[] = {
|
||||
{"version", no_argument, &opt_val, 1},
|
||||
{"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: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:
|
||||
std::cout << pw_version() << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'u':
|
||||
valid += upper;
|
||||
min_upper = std::atoi(optarg);
|
||||
if (min_upper < 0) throw std::runtime_error("minimum amout of upper case must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'l':
|
||||
valid += lower;
|
||||
min_lower = std::atoi(optarg);
|
||||
if (min_upper < 0) throw std::runtime_error("minimum amout of lower case must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'd':
|
||||
valid += digits;
|
||||
min_digits = std::atoi(optarg);
|
||||
if (min_upper < 0) throw std::runtime_error("minimum amout of digits must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 's':
|
||||
valid += symbols;
|
||||
min_symbols = std::atoi(optarg);
|
||||
if (min_upper < 0) throw std::runtime_error("minimum amout of symbols must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'S':
|
||||
special = arg;
|
||||
valid += arg;
|
||||
min_special = 1;
|
||||
noflags = false;
|
||||
break;
|
||||
case 'c':
|
||||
len = std::stoi(optarg);
|
||||
break;
|
||||
case '?':
|
||||
throw std::runtime_error("unrecognized option");
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
bool noflags{true};
|
||||
int opt_char, opt_val;
|
||||
struct option long_options[] = {
|
||||
{"version", no_argument, &opt_val, 1},
|
||||
{"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: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:
|
||||
std::cout << pw_version() << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (noflags) {
|
||||
valid = upper + lower + digits + symbols;
|
||||
}
|
||||
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::string password;
|
||||
for (int i = 0; i < min_digits; ++i) {
|
||||
password += sc::random::choice<char>(digits.begin(), digits.end());
|
||||
}
|
||||
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;
|
||||
break;
|
||||
}
|
||||
case 'u':
|
||||
valid += upper;
|
||||
min_upper = std::atoi(optarg);
|
||||
if (min_upper < 0)
|
||||
throw std::runtime_error(
|
||||
"minimum amout of upper case must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'l':
|
||||
valid += lower;
|
||||
min_lower = std::atoi(optarg);
|
||||
if (min_upper < 0)
|
||||
throw std::runtime_error(
|
||||
"minimum amout of lower case must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'd':
|
||||
valid += digits;
|
||||
min_digits = std::atoi(optarg);
|
||||
if (min_upper < 0)
|
||||
throw std::runtime_error(
|
||||
"minimum amout of digits must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 's':
|
||||
valid += symbols;
|
||||
min_symbols = std::atoi(optarg);
|
||||
if (min_upper < 0)
|
||||
throw std::runtime_error(
|
||||
"minimum amout of symbols must be zero or more");
|
||||
noflags = false;
|
||||
break;
|
||||
case 'S':
|
||||
special = arg;
|
||||
valid += arg;
|
||||
min_special = 1;
|
||||
noflags = false;
|
||||
break;
|
||||
case 'c':
|
||||
len = std::stoi(optarg);
|
||||
break;
|
||||
case '?':
|
||||
throw std::runtime_error("unrecognized option");
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
if (noflags) {
|
||||
valid = upper + lower + digits + symbols;
|
||||
}
|
||||
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::string password;
|
||||
for (int i = 0; i < min_digits; ++i) {
|
||||
password += sc::random::choice<char>(digits.begin(), digits.end());
|
||||
}
|
||||
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;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,18 +2,18 @@
|
||||
#include <sstream>
|
||||
|
||||
std::string pw_version() {
|
||||
#include "version.inc"
|
||||
#include "commit.inc"
|
||||
std::ostringstream oss;
|
||||
oss << "pw version " << version;
|
||||
#include "version.inc"
|
||||
std::ostringstream oss;
|
||||
oss << "pw version " << version;
|
||||
#ifdef DEBUG
|
||||
oss << " DEBUG";
|
||||
oss << " DEBUG";
|
||||
#endif
|
||||
oss << '\n';
|
||||
if (commit[0] != '\0') {
|
||||
oss << "build " << commit << ", ";
|
||||
}
|
||||
oss << __DATE__ << ", " << __TIME__ << '\n';
|
||||
oss << "(c) Bob Polis, all rights reserved";
|
||||
return oss.str();
|
||||
oss << '\n';
|
||||
if (commit[0] != '\0') {
|
||||
oss << "build " << commit << ", ";
|
||||
}
|
||||
oss << __DATE__ << ", " << __TIME__ << '\n';
|
||||
oss << "(c) Bob Polis, all rights reserved";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user