-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomPassword.cpp
38 lines (32 loc) · 956 Bytes
/
randomPassword.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
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
using namespace std;
string generatePassword(int length)
{
const string characters = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
"!@#$%^&*()_+-=[]{}|;:,.<>?";
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, characters.size() - 1);
string password;
password.resize(length);
generate(password.begin(), password.end(), [&]() { return characters[dis(gen)];});
return password;
}
int main()
{
int length;
cout << "Enter the wanted length for the password: ";
cin >> length;
if (length <= 0)
{cout << "Password length must be greater than 0.\n";
return 1;
}
string password = generatePassword(length);
cout << "Generated password: " << password << endl;
return 0;
}