-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_manager.cpp
More file actions
106 lines (92 loc) · 4.1 KB
/
password_manager.cpp
File metadata and controls
106 lines (92 loc) · 4.1 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
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @file password_manager.cpp
* @brief Implementation of the PasswordManager class.
*
* This file contains the definitions of the methods declared in the PasswordManager class.
* It includes functionalities for generating secure passwords, managing user-defined options,
* and parsing command-line arguments. Each method is designed to handle specific tasks
* related to password management.
*
* Ensure that this file is compiled along with main.cpp to create the complete password generator application.
*/
#include "include.h"
#include "password_manager.h"
STATUS_CODE PasswordManager::generateSecurePassword( int length, bool useUpper, bool useLower, bool useNums, bool useSymbols ) {
const std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string lower = "abcdefghijklmnopqrstuvwxyz";
const std::string digits = "0123456789";
const std::string symbols = "!@#$%^&*()-_=+[{]};:<>?|";
std::string password = "";
if ( length < 12 || length > 128 ) {
std::cerr << "Error: Invalid length, value must be between 12 and 128." << std::endl;
return NOT_ENOUGH_LENGTH;
}
std::string allowedChars = "";
if ( useUpper ) allowedChars += upper;
if ( useLower ) allowedChars += lower;
if ( useNums ) allowedChars += digits;
if ( useSymbols ) allowedChars += symbols;
if ( allowedChars.empty( ) ) {
std::cerr << "Error: No character type was chosen. Unable to generate a secure password." << std::endl;
return NO_CHAR_TYPE_SELECTED;
}
std::random_device randomDev;
std::mt19937 gen( randomDev( ) );
std::uniform_int_distribution<> dis( 0, allowedChars.length( ) - 1 );
for ( int i = 0; i < length; ++i ) {
int index = dis( gen );
password += allowedChars[ index ];
}
setPassword( password );
return STATUS_SUCCESS;
}
void PasswordManager::setPassword( std::string password ) {
securePassword = password;
}
const std::string PasswordManager::getSecurePassword( ) {
return securePassword;
}
void PasswordManager::showHelp( ) {
std::cout << "Use: ./secure_password_gen [options]" << std::endl;
std::cout << "Available Options:" << std::endl;
std::cout << " -u, --upper Include uppercase chars" << std::endl;
std::cout << " -m, --lower Include lowercase chars" << std::endl;
std::cout << " -n, --numbers Include numbers" << std::endl;
std::cout << " -s, --symbols Include spceial symbols" << std::endl;
std::cout << " -l [n], --length [n] Password length (min 8 chars)" << std::endl;
std::cout << " -h, --help Show help screen and exit" << std::endl;
}
void PasswordManager::parseArguments( int argc, char* argv[ ], int& length, bool& upper, bool& lower, bool& nums, bool& symbols ) {
for ( int i = 1; i < argc; ++i ) {
if ( strcmp( argv[ i ], "-h" ) == 0 || strcmp( argv[ i ], "--help" ) == 0 ) {
showHelp( );
exit( 0 );
}
else if ( strcmp( argv[ i ], "-u" ) == 0 || strcmp( argv[ i ], "--upper" ) == 0 ) {
upper = true;
}
else if ( strcmp( argv[ i ], "-m" ) == 0 || strcmp( argv[ i ], "--lower" ) == 0 ) {
lower = true;
}
else if ( strcmp( argv[ i ], "-n" ) == 0 || strcmp( argv[ i ], "--numbers" ) == 0 ) {
nums = true;
}
else if ( strcmp( argv[ i ], "-s" ) == 0 || strcmp( argv[ i ], "--symbols" ) == 0 ) {
symbols = true;
}
else if ( strcmp( argv[ i ], "-l" ) == 0 || strcmp( argv[ i ], "--length" ) == 0 ) {
if ( i + 1 < argc ) {
length = std::atoi( argv[ ++i ] );
}
else {
std::cerr << "Error: A number was expected after -l / --length" << std::endl;
exit( 1 );
}
}
else {
std::cerr << "Unknown Input: " << argv[ i ] << std::endl;
std::cout << "Input -h or --help to get a guide on how to use this program!" << std::endl;
exit( 1 );
}
}
}