-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatepassword.cpp
More file actions
66 lines (59 loc) · 2.05 KB
/
createpassword.cpp
File metadata and controls
66 lines (59 loc) · 2.05 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
#include "createpassword.h"
std::string createPassword( const AllowedCharacters& allowedCharacters, std::size_t length )
{
std::string password;
if( !allowedCharacters.letters &&
!allowedCharacters.numbers &&
!allowedCharacters.specials ) //nothing allowed
{
return password;
}
std::mt19937 rng;
rng.seed( time( nullptr ) );
while( password.length() != length )
{
std::uniform_int_distribution< int32_t > distForChoice( 1, 4 );
auto choice( distForChoice( rng ) );
if ( choice == 1 && allowedCharacters.letters ) //small letters
{
std::uniform_int_distribution< int32_t > distForSmallLetters( 97, 122 );
password += static_cast< char >( distForSmallLetters( rng ) );
}
if ( choice == 2 && allowedCharacters.letters ) //big letters
{
std::uniform_int_distribution< int32_t > distForBigLetters( 65, 90 );
password += static_cast< char >( distForBigLetters( rng ) );
}
if ( choice == 3 && allowedCharacters.numbers ) //numbers 0-9
{
std::uniform_int_distribution< int32_t > distForNumbers( 0, 9 );
password += std::to_string( distForNumbers( rng ) );
}
if ( choice == 4 && allowedCharacters.specials ) //special characters
{
std::uniform_int_distribution< int32_t > distForSpecialCharacters( 1, 5 );
auto specialCharactersChoice( distForSpecialCharacters( rng ) );
if( specialCharactersChoice == 1 )
{
password += '!';
}
if( specialCharactersChoice == 2 )
{
password += '?';
}
if( specialCharactersChoice == 3 )
{
password += '#';
}
if( specialCharactersChoice == 4 )
{
password += '$';
}
if( specialCharactersChoice == 5 )
{
password += '&';
}
}
}
return password;
}