Change indent to 2 spaces
This commit is contained in:
74
src/main.cpp
74
src/main.cpp
@@ -1,30 +1,29 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <string>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <climits>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <getopt.h>
|
|
||||||
#include <libscnumerics.hpp>
|
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <libscnumerics.hpp>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
// globals
|
// globals
|
||||||
std::string upper {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
std::string upper{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
||||||
std::string lower {"abcdefghijklmnopqrstuvwxyz"};
|
std::string lower{"abcdefghijklmnopqrstuvwxyz"};
|
||||||
std::string digits {"0123456789"};
|
std::string digits{"0123456789"};
|
||||||
std::string symbols {"_-=+<>,.!@#$%^&*"};
|
std::string symbols{"_-=+<>,.!@#$%^&*"};
|
||||||
std::string special;
|
std::string special;
|
||||||
std::string valid;
|
std::string valid;
|
||||||
int len {24};
|
int len{24};
|
||||||
int min_upper {0};
|
int min_upper{0};
|
||||||
int min_lower {0};
|
int min_lower{0};
|
||||||
int min_digits {0};
|
int min_digits{0};
|
||||||
int min_symbols {0};
|
int min_symbols{0};
|
||||||
int min_special {0};
|
int min_special{0};
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
try {
|
try {
|
||||||
bool noflags {true};
|
bool noflags{true};
|
||||||
int opt_char, opt_val;
|
int opt_char, opt_val;
|
||||||
struct option long_options[] = {
|
struct option long_options[] = {
|
||||||
{"version", no_argument, &opt_val, 1},
|
{"version", no_argument, &opt_val, 1},
|
||||||
@@ -34,10 +33,10 @@ int main(int argc, char* argv[]) {
|
|||||||
{"symbol", required_argument, nullptr, 's'},
|
{"symbol", required_argument, nullptr, 's'},
|
||||||
{"special", required_argument, nullptr, 'S'},
|
{"special", required_argument, nullptr, 'S'},
|
||||||
{"upper", required_argument, nullptr, 'u'},
|
{"upper", required_argument, nullptr, 'u'},
|
||||||
{nullptr, 0, nullptr, 0}
|
{nullptr, 0, nullptr, 0}};
|
||||||
};
|
while ((opt_char = getopt_long(argc, argv, "c:u:l:d:s:S:", long_options,
|
||||||
while ((opt_char = getopt_long(argc, argv, "c:u:l:d:s:S:", long_options, nullptr)) != -1) {
|
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
|
||||||
@@ -51,25 +50,33 @@ int main(int argc, char* argv[]) {
|
|||||||
case 'u':
|
case 'u':
|
||||||
valid += upper;
|
valid += upper;
|
||||||
min_upper = std::atoi(optarg);
|
min_upper = std::atoi(optarg);
|
||||||
if (min_upper < 0) throw std::runtime_error("minimum amout of upper case must be zero or more");
|
if (min_upper < 0)
|
||||||
|
throw std::runtime_error(
|
||||||
|
"minimum amout of upper case must be zero or more");
|
||||||
noflags = false;
|
noflags = false;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
valid += lower;
|
valid += lower;
|
||||||
min_lower = std::atoi(optarg);
|
min_lower = std::atoi(optarg);
|
||||||
if (min_upper < 0) throw std::runtime_error("minimum amout of lower case must be zero or more");
|
if (min_upper < 0)
|
||||||
|
throw std::runtime_error(
|
||||||
|
"minimum amout of lower case must be zero or more");
|
||||||
noflags = false;
|
noflags = false;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
valid += digits;
|
valid += digits;
|
||||||
min_digits = std::atoi(optarg);
|
min_digits = std::atoi(optarg);
|
||||||
if (min_upper < 0) throw std::runtime_error("minimum amout of digits must be zero or more");
|
if (min_upper < 0)
|
||||||
|
throw std::runtime_error(
|
||||||
|
"minimum amout of digits must be zero or more");
|
||||||
noflags = false;
|
noflags = false;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
valid += symbols;
|
valid += symbols;
|
||||||
min_symbols = std::atoi(optarg);
|
min_symbols = std::atoi(optarg);
|
||||||
if (min_upper < 0) throw std::runtime_error("minimum amout of symbols must be zero or more");
|
if (min_upper < 0)
|
||||||
|
throw std::runtime_error(
|
||||||
|
"minimum amout of symbols must be zero or more");
|
||||||
noflags = false;
|
noflags = false;
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
@@ -88,9 +95,11 @@ int main(int argc, char* argv[]) {
|
|||||||
if (noflags) {
|
if (noflags) {
|
||||||
valid = upper + lower + digits + symbols;
|
valid = upper + lower + digits + symbols;
|
||||||
}
|
}
|
||||||
int num_free_choice = len - min_digits - min_lower - min_upper - min_symbols;
|
int num_free_choice =
|
||||||
|
len - min_digits - min_lower - min_upper - min_symbols;
|
||||||
if (num_free_choice < 0) {
|
if (num_free_choice < 0) {
|
||||||
throw std::runtime_error("total minimum required exceeds password length");
|
throw std::runtime_error(
|
||||||
|
"total minimum required exceeds password length");
|
||||||
}
|
}
|
||||||
std::string password;
|
std::string password;
|
||||||
for (int i = 0; i < min_digits; ++i) {
|
for (int i = 0; i < min_digits; ++i) {
|
||||||
@@ -111,9 +120,10 @@ int main(int argc, char* argv[]) {
|
|||||||
for (int i = 0; i < num_free_choice; ++i) {
|
for (int i = 0; i < num_free_choice; ++i) {
|
||||||
password += sc::random::choice<char>(valid.begin(), valid.end());
|
password += sc::random::choice<char>(valid.begin(), valid.end());
|
||||||
}
|
}
|
||||||
std::shuffle(password.begin(), password.end(), sc::random::instance().engine());
|
std::shuffle(password.begin(), password.end(),
|
||||||
|
sc::random::instance().engine());
|
||||||
std::cout << password << std::endl;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
std::string pw_version() {
|
std::string pw_version() {
|
||||||
#include "version.inc"
|
|
||||||
#include "commit.inc"
|
#include "commit.inc"
|
||||||
|
#include "version.inc"
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "pw version " << version;
|
oss << "pw version " << version;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
|||||||
Reference in New Issue
Block a user