forked from Zhu-Xinrong/single
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.cpp
More file actions
94 lines (94 loc) · 3.65 KB
/
password_generator.cpp
File metadata and controls
94 lines (94 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <flat_map>
#include <numeric>
#include <print>
#include <random>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
using namespace std::string_view_literals;
auto make_password( const std::size_t length, const std::vector< char >& dic )
{
std::string password( length, '\0' );
std::mt19937_64 rng{ std::random_device{}() };
std::uniform_int_distribution< std::size_t > dist( 0, dic.size() - 1 );
for ( auto& e : password ) {
e = dic[ dist( rng ) ];
}
return password;
}
auto show_help_info() noexcept
{
std::print(
"[ Password Generator ]\n"
"Supported arguments:\n"
" '--no-capital-letters': Remove capital letters from the dictionary.\n"
" '--no-lowercase-letters': Remove lowercase letters from the dictionary.\n"
" '--no-numbers': Remove numbers from the dictionary.\n"
" '--no-special-characters': Remove special characters from the dictionary.\n"
" '--password-length=[a positive integer]': Set the length of a single password.\n"
" '--number-of-passwords=[a positive integer]': Set the number of passwords to generate.\n"
"NOTE: The dictionary cannot be empty!\n" );
}
auto main( const int argc, const char* const args[] ) -> int
{
constexpr auto error_info{ "Arguments failed! Please use '--help' to view the usage guide.\n" };
std::flat_map< std::string_view, bool > options{
{"--no-capital-letters"sv, false},
{"--no-lowercase-letters"sv, false},
{"--no-numbers"sv, false},
{"--no-special-characters"sv, false}
};
std::flat_map< std::string_view, long long > settings{
{"--password-length="sv, 16},
{"--number-of-passwords="sv, 1 }
};
for ( int i{ 1 }; i < argc; ++i ) {
const std::string_view current_args{ args[ i ] };
if ( current_args == "--help"sv ) {
show_help_info();
return EXIT_SUCCESS;
}
if ( options.contains( current_args ) ) {
options[ current_args ] = true;
continue;
}
const std::string_view settings_name{ current_args.begin(), std::ranges::find( current_args, '=' ) + 1 };
if ( settings.contains( settings_name ) ) {
settings[ settings_name ] = std::stoll( std::ranges::find( current_args, '=' ) + 1 );
continue;
}
std::print( error_info );
return EXIT_FAILURE;
}
std::vector< char > dic;
if ( !options[ "--no-capital-letters"sv ] ) {
for ( const auto c : std::ranges::iota_view{ 'A', 'Z' + 1 } ) {
dic.emplace_back( c );
}
}
if ( !options[ "--no-lowercase-letters"sv ] ) {
for ( const auto c : std::ranges::iota_view{ 'a', 'z' + 1 } ) {
dic.emplace_back( c );
}
}
if ( !options[ "--no-numbers"sv ] ) {
for ( const auto c : std::ranges::iota_view{ '1', '9' + 1 } ) {
dic.emplace_back( c );
}
}
if ( !options[ "--no-special-characters"sv ] ) {
for ( const auto c : R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|})"sv ) {
dic.emplace_back( c );
}
}
const auto password_length{ settings[ "--password-length="sv ] };
const auto num_of_passwords{ settings[ "--number-of-passwords="sv ] };
if ( num_of_passwords <= 0 || password_length <= 0 || dic.size() == 0 ) {
std::print( error_info );
return EXIT_FAILURE;
}
for ( std::remove_const_t< decltype( num_of_passwords ) > _{ 0 }; _ < num_of_passwords; ++_ ) {
std::print( "{}\n", make_password( password_length, dic ) );
}
}