forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passGenerator.cpp
39 lines (30 loc) · 1 KB
/
passGenerator.cpp
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
// Function to generate a random password
std::string GeneratePassword(int length) {
const std::string charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
const int charsetLength = charset.length();
std::string password;
for (int i = 0; i < length; ++i) {
int randomIndex = rand() % charsetLength;
password += charset[randomIndex];
}
return password;
}
int main() {
// Seed the random number generator with the current time
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int passwordLength;
std::cout << "Enter the desired password length: ";
std::cin >> passwordLength;
if (passwordLength <= 0) {
std::cout << "Invalid password length." << std::endl;
return 1;
}
std::string password = GeneratePassword(passwordLength);
std::cout << "Generated Password: " << password << std::endl;
return 0;
}